📉ConsoleBase - NKS0002

Use ConsoleWrapper instead of Console

This analyzer provides the following strings:

ContextString

Error List

Caller uses Console instead of ConsoleWrapper

Suggestion Box

Use ConsoleWrapper instead of Console

Description

ConsoleWrapper makes sure that your console is not a dumb console. This class is a wrapper for the Console class so that it works cross-platform, while Console contains some platform-dependent APIs.

Extended Description

This code analyzer detects the usage of the standard Console class found in the System namespace. However, this class is a sealed class, so it's rendered inextensible, causing you to have to make its abstraction class to wrap its functions if you want to add extra code, like checking for VT sequences.

Fortunately, Nitrocid KS has its own console wrapper, ConsoleWrapper, that makes use of the current local console driver that contains the necessary checks, like checking the console for its fullness (positioning and any other functions other than plain writing).

So, if you want to use the Console class, you must replace these calls with ConsoleWrapper to take advantage of the local console drivers.

Furthermore, some of the wrapper functions are used by the much powerful console writers, like TextWriterColor.Write(), that include sanity checks before actually doing the job.

Analysis Comparison

To get a brief insight about how this analyzer works, compare the two code blocks shown to you below:

Before the fix

Somewhere in your mod code...
public static void MyFunction() =>
    Console.WriteLine("Hello, world!");

After the fix

Somewhere in your mod code...
public static void MyFunction() =>
    ConsoleWrapper.WriteLine("Hello, world!");

Suppression

You can suppress this warning by including it in the appropriate place, whichever is convenient.

For more information about how to suppress any warning issued by the Nitrocid analyzer, visit the below page:

Recommendation

We recommend that every caller which use the Console class use the recommended abovementioned class.

Last updated