Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Antipatterns
    text
    copied!<p>To cut a long story short: I find the <a href="http://www.odi.ch/prog/design/newbies.php" rel="nofollow noreferrer">Java antipatterns</a> an indispensable resource. For beginners as much as for professionals. I have yet to find something like this for C#. So I'll open up this question as community wiki and invite everyone to share their knowledge on this. As I am new to C#, I am strongly interested in this, but cannot start with some antipatterns :/</p> <p>Here are the answers which I find specifically true for C# and not other languages.</p> <p><em>I just copy/pasted these! Consider throwing a look on the comments on these as well.</em></p> <hr> <h1><a href="https://stackoverflow.com/questions/1529604/c-antipatterns/1529674#1529674">Throwing <code>NullReferenceException</code></a></h1> <p>Throwing the wrong exception:</p> <pre><code>if (FooLicenceKeyHolder == null) throw new NullReferenceException(); </code></pre> <hr> <h1><a href="https://stackoverflow.com/questions/1529604/c-antipatterns/1529619#1529619">Properties vs. public Variables</a></h1> <p>Public variables in classes (use a property instead).</p> <p><strong>Unless</strong> the class is a simple Data Transfer Object.</p> <hr> <h1><a href="https://stackoverflow.com/questions/1529604/c-antipatterns/1529797#1529797">Not understanding that bool is a real type, not just a convention</a></h1> <pre><code>if (myBooleanVariable == true) { ... } </code></pre> <p>or, even better</p> <pre><code>if (myBooleanVariable != false) { ... } </code></pre> <p>Constructs like these are often used by <code>C</code> and <code>C++</code> developers where the idea of a boolean value was just a convention (0 == false, anything else is true); this is not necessary (or desirable) in C# or other languages that have real booleans.</p> <hr> <h1><a href="https://stackoverflow.com/questions/1529604/c-antipatterns/1529636#1529636">Using <code>using()</code></a></h1> <p>Not making use of <code>using</code> where appropriate:</p> <pre><code>object variable; variable.close(); //Old code, use IDisposable if available. variable.Dispose(); //Same as close. Avoid if possible use the using() { } pattern. variable = null; //1. in release optimised away. 2. C# is GC so this doesn't do what was intended anyway. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload