C#

C#

By DeepSource

Calling garbage collector manually does not necessarily improve performanceCS-P1001
Performance

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:

Consider using `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`CS-P1002
Performance

Using 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.

Consider making `static readonly` fields `const`CS-P1003
Performance

Expressions marked as static readonly do not require an object/instance of a class and can be accessed directly. Since they're marked as static, they cannot be modified outside a static constructor. If such fields exist in a class without a static constructor, it is recommended that you mark them as const since expressions marked as const are fully evaluated at the compile-time.

Redundant conversion to an `Array` during iterationCS-P1004
Performance

Methods such as ToCharArray and ToArray return an array representation of the element. For example, invoking ToCharArray on a string returns char[], i.e. a char array. Invoking such methods during iteration is redundant and is an extra overhead. Consider dropping such explicit calls.

Consider using `.TryGetValue` to access elements in `Dictionary`CS-P1005
Performance

The .ContainsKey method allows you to check if a key exists in a Dictionary while the indexer, i.e. [] allows you to directly access the specified key's value. Using both of them means that you'd be effectively accessing the Dictionary twice. As an alternative, you can use .TryGetValue to get a value from a Dictionary that returns a bool depending on whether the key exists or not.