Empty blocks do not necessarily add any value to the codebase and should be discarded if possible. Therefore, it is recommended that you either discard this empty block or refactor it accordingly.
for
loop due to stackalloc
CS-W1025A stackalloc
expression allocates a block of memory on the stack and is cleaned up only when the controller exits the method. Placing a stackalloc
expression inside a loop continuously allocates memory and does not let it get discarded despite the controller exiting the loop's scope. It is therefore recommended that you rethink your approach.
TestMethod
declared in a non-test class CS-W1013Test methods are usually denoted via the [TestMethod]
attribute. Such methods must be declared in classes with the [TestClass]
attribute. Failing to do so might result in the test-suite not executing the said test method. It is therefore recommended that you either drop the [TestMethod]
altogether, or add the [TestClass]
attribute to the class containing this test.
NotImplementedException
CS-A1003NotImplementedException
is usually thrown to indicate that a particular feature or functionality is not yet implemented thus limiting the overall functionality of your application. This check lets you track all such instances across your codebase.
Preprocessor directives allow you to suppress warnings for a specific section of the code. This is usually done either because the warning is a false positive or that the code itself is flawed. In case this is a valid warning, consider addressing the root cause behind it rather than suppressing it.
Using Environment.Exit(0)
terminates the application abruptly in a potentially unsafe manner. An application should always try to quit gracefully irrespective of whether it encounters an error or not. If your application has to terminate, ensure that it implements the required cleanup/dispose methods to at least safely dispose off the resources that it has acquired/locked to the extent possible and then exit cleanly/gracefully.
L
as a suffix for long
rather than l
CS-R1027Using l
(lowercase L
) as a suffix to a numeric value indicates that it should be treated as long
. However, in some cases, the lowercase L
may look similar to the digit 1
. It is therefore recommended that you use L
as the suffix to avoid confusion.
Methods can be marked as obsolete via the [Obsolete]
attribute. Doing so produces a warning to the user along with an explanation why it was marked as obsolete. A method may be marked as obsolete for numerous reasons such as performance, security, or due to the fact that a more reliable alternative exists. It is therefore recommended that you do not use such obsolete methods and move to a more modern and reliable alternatives.
IndexOutOfRangeException
CS-W1042Languages like Python allow you to access elements from the last by specifying negative indices. However, this does not work in C# and will result in an IndexOutOfRangeException
. In order to access elements from the last, consider prefixing the indices with ^
operator.
The [Ignore]
attribute is used to ignore a specific test's final result. This is usually done in situations where the test is failing, either due to the fault in the test itself, or that the logic that is being tested is at fault and is not behaving as per requirement. Because this indicates a potential flaw within your application, it is recommended that you fix it as soon as possible.
Exception
s are instantiated just like normal classes via the new
keyword and then thrown using the throw
keyword. However, in this case, the Exception
that is being created is neither thrown nor used in any other manner. It is recommended that you either get rid of this statement completely or throw the Exception
.
Statements placed after the return
statement are not executed and is considered as "dead" code. Either adding the return
statement was a mistake or that the code placed after the return
statement wasn't supposed to be there in the first place. Either way, it is recommended that you rectify this immediately.
double.IsNaN()
to check if a double
is NaN
CS-W1003By specification, NaN
is not equal to anything, not even itself. Therefore, comparing any double
with NaN
will always evaluate to false
. The preferred and right approach is to directly use the double's .isNaN
method.
Any new keyword added to the C# language will only contain lowercase ASCII characters. It is therefore recommended that your types contain a non-lower ASCII character such as an upper-case character, a numeric, or an underscore. Failing to meet this requirement might cause a clash if C# adds a keyword with the same name in the newer releases, which would block you from migrating to a newer C# version.
enum
used for permissions is missing the Flags
attribute CS-A1004Flags
attribute is generally used when representing multiple values often via bitwise operators. It is recommended that enum
s used in implementing permissions use this attribute.
Keywords such as async
and await
have been introduced in C# 5.0 and are called contextual keywords. Contextual keywords have special meaning only in a limited program context and can be used as identifiers outside that context. However, it is recommended that you do not use such keywords as identifiers to avoid confusion.
String interpolation requires that you specify the string that is to be interpolated and the required arguments. Failing to specify the required arguments may result in an ill-formatted string. Because it is possible that such strings can critically alter the behaviour and control-flow of the program, it is suggested that you fix it as soon as possible.
Nested switch statements can affect code readability, refactoring and can cause additional bugs to creep in. Therefore, it is recommended that you refactor your code accordingly, i.e., either avoid nested switch
es or move the inner switch
to a separate method/lambda.
Thread
ing APIs CS-W1002Thread
ing APIs such as CurrentThread.Resume()
and CurrentThread.Suspend()
are deprecated and no longer maintained. It is therefore recommended that you switch to other classes in System.Threading
, such as Monitor
, Mutex
, Event
, or Semaphore
, to synchronize Threads or protect resources.
Exception
s should always be public
CS-W1004Exception
s are not limited to a part of the codebase and can be thrown and caught anywhere. It is therefore recommended that your custom Exception
s always be declared as public
. Otherwise, you'd have to catch
the closest accessible public
exception.