How can you improve the performance of a c# application?
Optimizing the performance of a C# code, we can apply the following code practices:
- Make sure we're using the correct type or data structure based on the scenario. Using an Array in place of a List whenever possible can help, because List stores data in terms of objects in a dynamic array fashion, while an Array stores in a fixed length of sequential data.
- Use a for loop instead of a for-each loop. Because a for-each loop internally takes time to initialize and run an enumerator which is a performance overhead.
- Use a StringBuilder while performing string manipulations, because Strings are immutable and each time a string is manipulated a new String object is created.
- Use a struct over a class if possible, since struct is a value type which is faster than a class which is a reference type.
- Using a variable in place of a property when the property isn't necessarily required can help improve performance - since a property is internally a variable with additional functionality (setter and getter) added, which might not be required in all the cases.
Posted May 14, 2021