A conditional expression is an expression which contains the code that is to be executed depending on whether a condition is true or false. Having identical expressions for both the true and false branches is likely a mistake and can affect your program's execution path and is therefore recommended that you fix it.
for loop has 3 basic elements:
In this case, the inner
for loop is modifying a variable that belongs to the outer/enclosing
for loop. This can result in an undefined behavior such as infinite loop. It is therefore recommended that you modify the right variable to ensure that your loop terminates as required.
stringwill result in
Casting a generic array of type
object to a
string array will always fail even if all the elements are strings. It is therefore recommended that you use a suitable and correct alternative such as
System.Linq.Select to convert the elements.
lockstatements should be avoided CS-W1076
lock statements allow you to safely access a resource in a concurrent environment. However, there are certain risks associated with it such as properly acquiring and releasing a lock, deadlocks, etc. An empty lock statement acquires the lock and releases it almost immediately and is usually not considered a proper practice and should be replaced with a suitable replacement such as wait handles.
ifstatements should be avoided CS-W1077
If statements allow you to execute code depending on whether the specified condition evaluates to
false. An empty
if statement has no statements in both of its branches and effectively accomplishes nothing. Such statements should be avoided.
Synchronization allows you to safely access resources that may be subject to race conditions. If one of the accessors, getter, for example, utilizes
lock statements, it means that there is room for race condition and that the underlying resource may be concurrently accessible. In such cases, it is possible that there might be a similar race condition for the setter as well. It is therefore recommended that both accessors be mutually synchronized.
DateTime constructor allows you to specify the year, month, day, and time to construct a
DateTime object. However, this constructor throws an
ArgumentOutOfRangeException exception if the specified date is invalid.
The use of addition and subtraction operations can lead to the creation of invalid dates. Instead, it is recommended that you use APIs such as
AddYears() to construct a
DateTime object since these methods handle invalid dates more appropriately.
C# allows you to define operators just like normal methods. However, these operators should not refer to themselves in their implementation. Doing so may result in unterminated recursive calls.
forloop due to
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.
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.
NotImplementedException 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.
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.
TestMethoddeclared in a non-test class CS-W1013
Test 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.
The result of an expression is assigned to a variable. However, the very subsequent statement is an assignment expression that does not use this previously computed value. This succeeding statement ends up overwriting the previously assigned value. Either this was not meant to happen or the previous assignment expression is actually useless and can be removed.
DefaultParameterValuemust be used instead of
Optionalattribute is specified CS-W1045
Optional attribute signifies that the parameter is optional, meaning the caller does not have to pass its value explicitly. In such cases, it is ideal to specify a default value. However, it is easy to get confused with
DefaultParameterValue attributes as the former is used for fields rather than parameters. Therefore, consider using the
DefaultParameterValue attribute instead.
A setter simply assigns the user provided value to a field. To do this, it has to access
value, an identifier that has its own meaning in the context of an accessor. However, in this case, the setter has no reference to the identifier
value and is likely ignoring the value that the user is providing.
Las a suffix for
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.
Dividing a number by zero is undefined and results in
System.DivideByZeroException. There is no reason to do this and is likely a mistake.