C# - a few tips
November 23rd, 2006Consider marking as static - Methods that do not access instance data or call instance methods can be marked as static. The compiler will emit non-virtual call sites to static members. So it will prevent checking at runtime for each call whether the current object pointer is non-null. It can be a performance gain.
Do not initialize unnecessarily - In most cases, initializing a field to its default value in a constructor is not necessary. I can even say it’s redundant. So something like:
object foo = null;
is not necessary. The common language runtime will do it before running the constructor. I don’t need to add that it also degrades performance
Identifiers should differ by more than case - The names of two types, members or parameters cannot differ only by case. Some languages that target common language runtime are not required to be case-sensitive. For example Visual Basic. It is rather important when you write a library which will be used in other languages too.
