Effective C#: 50 Specific Ways to Improve Your C#

Category: Programming
Author: Bill Wagner
4.3
All Stack Overflow 19
This Month Stack Overflow 7

Comments

by Frederick   2019-07-21

You may find these two books useful:

  1. Effective C#
  2. More effective C#

They are specific to C#, but you'll certainly learn many good ideas that apply to any .NET language.

by anonymous   2019-07-21

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

by anonymous   2019-07-21

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.

by endpoint   2019-07-21
  • 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.)

by anonymous   2019-01-13

Solutions

You can use one of the following—

1: Conditional attribute

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:

    string sth = null;
#if DEBUG
    sth = "oh, hi!";
#endif
    Console.WriteLine(sth);

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.

by pbz   2017-08-20

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.

by anonymous   2017-08-20

How about Effective C#?

by anonymous   2017-08-20

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:

  1. Is the main responsability of the type data storage?
  2. Is its public interface defined entirely by properties that access or modify its data members?
  3. Are you sure your type will never have subclasses?
  4. Are you sure your type will never be treated polymorphically?

If you answer 'yes' to all 4 questions: use a struct. Otherwise, use a class.