Using is a cool way of cleaning up resources, it is equivalent to try{}catch{}finally{dispose}. Effective c# has an item on this and I bet you willget 10+ similar answers.
The const keyword can be used for compile time constants such as primitive types and strings
The readonly keyword can be used for run-time constants such as reference types
The problem with readonly is that it only allows the reference (pointer) to be constant. The thing referenced (pointed to) can still be modified. This is the tricky part but there is no way around it. To implement constant objects means making them not expose any mutable methods or properties but this is awkward.
See also Effective C#: 50 Specific Ways to Improve Your C# (Item 2 - Prefer readonly to const.)
The Conditional attribute indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.
Code example:
[Conditional("DEBUG")]
static void Method() { }
2: #if preprocessor directive
When the C# compiler encounters an #if preprocessor directive, followed eventually by an #endif directive, it compiles the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol. The #if statement in C# is Boolean and only tests whether the symbol has been defined or not.
Code example:
#if DEBUG
static int testCounter = 0;
#endif
3: Debug.Write methods
Debug.Write (and Debug.WriteLine) writes information about the debug to the trace listeners in the Listeners collection.
See also Debug.WriteIf and Debug.WriteLineIf.
Code example:
Debug.Write("Something to write in Output window.");
Notes
Beware of using #if directive since it can produce unintended situations in Release build. For example, see:
You may find these two books useful:
They are specific to C#, but you'll certainly learn many good ideas that apply to any .NET language.
Using is a cool way of cleaning up resources, it is equivalent to try{}catch{}finally{dispose}. Effective c# has an item on this and I bet you willget 10+ similar answers.
http://www.amazon.com/Effective-Specific-Ways-Improve-Your/dp/0321245660
Effective C# and More Effective C# by Bill Wagner. The second book deals with C# 2 and 3.
Either here and here on Amazon.com
or here and here on Amazon.co.uk
There's a new version of Effective C# (Amazon UK) that covers C# 4.0.
Beaten to it by Andy, but I was looking up the links.
The problem with readonly is that it only allows the reference (pointer) to be constant. The thing referenced (pointed to) can still be modified. This is the tricky part but there is no way around it. To implement constant objects means making them not expose any mutable methods or properties but this is awkward.
See also Effective C#: 50 Specific Ways to Improve Your C# (Item 2 - Prefer readonly to const.)
Solutions
You can use one of the following—
1:
Conditional
attributeThe
Conditional
attribute indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.Code example:
2:
#if
preprocessor directiveWhen the C# compiler encounters an
#if
preprocessor directive, followed eventually by an #endif directive, it compiles the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol. The #if statement in C# is Boolean and only tests whether the symbol has been defined or not.Code example:
3:
Debug.Write
methodsDebug.Write
(andDebug.WriteLine
) writes information about the debug to the trace listeners in the Listeners collection.See also
Debug.WriteIf
andDebug.WriteLineIf
.Code example:
Notes
Beware of using
#if
directive since it can produce unintended situations in Release build. For example, see:In this case, non-Debug build will print a blank message. But, this potentially may raise
NullReferenceException
in a different case.Read more
See also
There is also a tool, DebugView, which allow to capture debug information from external applications.
I would recommend Effective C# by Bill Wagner (first edition and second edition). He goes through a number of language constructs and techniques and explains which ones are faster and why. He touches on a lot of best practices as well.
More often than not, however, optimizing your algorithm will give you far better results than using any kind of language / optimization technique.
How about Effective C#?
Bill Wagner has a chapter about this in his book "effective c#" (http://www.amazon.com/Effective-Specific-Ways-Improve-Your/dp/0321245660). He concludes by using the following principle: