v0.50.1
Jul 20, 2024
303
97
version = 1
[[analyzers]]
name = "csharp"
Anti-pattern
146
Bug risk
101
Documentation
1
Performance
27
Security
28
Guid.Empty
to create an empty GUID CS-R1007new SomeClass()
is the syntax to instantiate a class in C#. However, new Guid()
does not generate a new GUID
. It instead returns an empty GUID
. If you intend to use an empty GUID
, consider using Guid.Empty
as it is more straightforward to comprehend. If you wish to generate a new usable GUID
, consider using Guid.NewGuid()
.
ToString
method should never return null
CS-R1011The ToString
method should return a string that best represents your class/object. Returning null
from such a method does not make any sense and is therefore recommended that you refactor this method to return a more suitable and appropriate representation.
Obsolete
attribute allows you to mark certain entities as obsolete, thereby discouraging users from using them. However, to convey the full context, it is recommended that you specify the obsolete-message/description. Failing to do so produces a blank line/empty string during the build process and can cause confusion.
StringComparison.OrdinalIgnoreCase
for case insensitive comparisons CS-R1017While converting string
s to lower/upper case and then comparing might work to perform a case insensitive comparison, the safer, reliable, and performant alternative is to invoke the string.Equals
method while specifying the StringComparison.OrdinalIgnoreCase
enum.
Individual elements in a string or an array can be accessed via the bracketed
expression. The norm for accessing elements from last is usually in the format of foo[ub - i]
where ub
is the upper bound and i
is an index. This expression however can be simplified and rewritten as foo[^i]
.