The .NET runtime ships with a GC (Garbage Collector) that is responsible for allocation and freeing up of memory as deemed necessary during an application's lifetime. It is responsible for automatically handling memory management-related tasks and preventing accidents such as memory leaks, accidental double frees, etc. Manually invoking the GC does not necessarily improve your application's performance and, in some cases, may even adversely impact the performance. If you wish to improve your application's performance, consider measures such as:
Methods such as string.Contains
and string.IndexOf
allow you to search for an occurrence of either a single char or a substring within a string
. If you'd like to search for an occurrence of a single char, it is recommended that you use the appropriate method that takes a char
as a parameter rather than a string
as the former approach is more performant and recommended.
.Substring
method CS-P1007Index lookup methods such as IndexOf
, LastIndexOf
, IndexOfAny
, and LastIndexOfAny
return the index of the char
depending on the arguments supplied. Chaining such calls to .Substring
requires that a part of the string be selected and then the char be looked up. Instead, consider calling the index lookup methods directly and then subtracting the begin offset.
Finalizers, i.e., destructors, perform clean-up operations as an instance is picked up for garbage collection (GC). If a class has a finalizer defined, it is added to the Finalize
queue, which is later processed by the GC when deemed appropriate. An empty finalizer adds unnecessary additional overhead to the GC since it does not perform effective clean-up operations. Therefore, it is suggested that you either remove the empty finalizer or add relevant clean-up operations.
Environment.ProcessId
instead of Process.GetCurrentProcess().Id
CS-P1002Using Process.GetCurrentProcess().Id
requires that an instance of Process
be allocated to access the required Id
. It then adds additional overhead, i.e., disposing of the Process
instance. It is therefore suggested that you use the reliable and performant alternative Environment.ProcessId
.