Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries
All
Stack Overflow 48
This Year
Stack Overflow 2
This Month
Stack Overflow 14
The Framework Design Guidelines book (first edition) by Krzysztof Cwalina and Brad Abrams recommend throwing existing exceptions defined in the
System
namespaces where you can, and the more specific the better. If there isn't a good fit then throw a custom exception.Creating a parallel universe of Custom
ArgumentNullException
to matchSystem.ArgumentNullException
is a duplication of effort that I don't see any value in. At the end of the day if your code throws aSystem.ArgumentNullException
rather than a framework class you can determine from the stack trace who was ultimately responsible.This smells of unnecessary extra work for the present and in the future when it comes to code maintenance time.
Sounds like you could benefit (and enjoy!) from reading this...
http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756
Example 2 is without a doubt the least error prone approach. Please see this answer I gave to a similar question for the reason why:
What is the prefered style for single decision and action statements?
Although the Visual Studio default for brace usage is to put braces on a newline (my preferred method), the Framework Design Guidelines book (first edition) by Krzysztof Cwalina and Brad Abrams propose a different convention, example 4, placing the opening brace at the end of a preceding
if
statement (Page 274). They also state "Avoid omitting braces, even if the language allows it".Not having the second edition at hand, I couldn't say if these conventions have changed or not.
These were in the Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries book, first published in 2005 - there are relevant excerpts on MSDN. These set to deprecate Hungarian notation, as that had got misused over the years for exposed names in .Net frameworks.
No it is not common to use getter /setter style names in C#. Properties should be used for almost all places you would use a getter / setter in Java.
IMHO, the defacto standard for naming conventions comes from the Framework Design Guidelines. It's enforced by several tools (FxCop) and is the dominant style of many libraries including the BCL.
I want to clarify this situation.
According to Microsoft guidelines, it's a good practice to provide
Close
method where suitable. Here is a citation from Framework design guidelinesIn most of cases
Close
andDispose
methods are equivalent. The main difference betweenClose
andDispose
in the case ofSqlConnectionObject
is:That said:
Dispose
.Close
method.The Framework Design Guidelines suggest returning a copy of the Array. That way, consumers can't change items from the array.
these are better:
The examples are straight from the book.
Dupe: there is another question on SO just like this: Abstract class constructor access modifier
The answers on that question come down to the same thing in the end: it does not really matter if you declare it
protected
orpublic
.Also there seems to be some discussion about it in literature (e.g. in Framework Design Guidelines). This is referenced in this blogpost: Good design or bad design of abstract class?
From Microsoft Constructor Design Guidelines (MSDN),
Factory Method is not the right way to approach this problem. See Constructors vs Factory Methods
From Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries
.NET BCL implementations do throw exceptions from constructors
For example, the List Constructor (Int32), throws an ArgumentOutOfRangeException when the capacity argument of the list is negative.
Similarly, your constructor should throw an appropriate type of exception when it reads the file. For example, it could throw FileNotFoundException if the file does not exist at the specified location, etc.
More Information
According to MS standards your code is OK. Having prefixes as m_ is not really necessary when you have advanced IDE. However short prefix like _ can be used to take advantage of auto complete feature to quickly sort out class members.
I would recommend you to get a copy of "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" book to learn more on MS standards
The significant point is that the variable name "should not" represent its type. Instead, It should indicate the "business semantic" of the variable; The type of the variable is subject to change during the code maintenance, but the semantics of that variable is rarely changed.
Incorporating "StyleCop" into your development lifecycle can enforce consistent code style amongst team members.
UPDATE: This excerpt from the Chapter 3 of "Framework Design Guidelines" which is dedicated to "Naming Guidelines" helps to clarify the issue:
The short answer is: nowhere.
It is a relic of the past, where Microsoft intended developers to inherit all their custom exceptions from ApplicationException. Shortly after, they changed their mind and advised that custom exceptions should derive from the base Exception class. See Best Practices for Handling Exceptions on MSDN.
One of the more widely circulated reasons for this comes from an exerpt from Jeffery Richter in Framework Design Guidelines:
So there you have it. The executive summary is that ApplicationException is not harmful, just useless.