I am reading Eric's blog series on static constructor and thought came in mind that as instance constructors can be overloaded,why static constructor cannot be overloaded? What is the reason behind this not providing the same ?
-
Any link for the Eric's blog maby please ? (:dotixx– dotixx2013-07-31 18:25:30 +00:00Commented Jul 31, 2013 at 18:25
-
Who is Eric? A link to his blog would help us understand your question better.Geeky Guy– Geeky Guy2013-07-31 18:26:16 +00:00Commented Jul 31, 2013 at 18:26
-
2Where would the argument come from?Eric Lippert– Eric Lippert2013-07-31 18:26:21 +00:00Commented Jul 31, 2013 at 18:26
-
2@dotixx: Use ericlippert.com/2013/02/06/static-constructors-part-one and follow links therein for remaining three parts (four parts total).jason– jason2013-07-31 18:26:34 +00:00Commented Jul 31, 2013 at 18:26
-
@renan I'll bet if you type Eric c# static constructor blog into your favorite search engine you'll find it.Eric Lippert– Eric Lippert2013-07-31 18:27:31 +00:00Commented Jul 31, 2013 at 18:27
3 Answers
Because you can never invoke a static constructor directly; it's always done implicitly for you by the runtime. Therefore, you can't pass parameters to a static constructor; therefore, the only possible static constructor is one with default parameters.
5 Comments
static constructor has run (which, because of the way it's implemented right now, you are guaranteed that it has run before you attempt to set the state).The static constructor cannot take a parameter. This means that you can't create a different method signature which prevents an overload.
2 Comments
Asking "why" a particular language feature was designed that way often leads to matters of opinion, which is why these kinds of questions are discouraged on Stack Overflow. That said, sometimes an answer, even if it's just a matter of opinion, can lead to a deeper understanding. Understand, I'm not on the C# development team or affiliated with Microsoft in any way, so you can take my answer with a grain of salt.
The job of the static constructor is to initialize any static data that's required for the class to function. The design of C# is such that the static constructor is guaranteed to execute before any instance of the class is created, and before any static methods are called or static properties accessed.
It's easy to write simple programs in such a way that it seems like you could make that guarantee manually--by making the application call the static constructor as one of the first things it does. But it doesn't require a very complex program to make it difficult to ensure that all static constructors are called before instances of the class are first used.
For example, say you include a third party component called Foo that has a static constructor. In order to use it, your application would have to call the Foo static constructor before doing anything else. And the Foo constructor would have to be written so that it called the static constructor for every class that it uses. And what happens if the Foo static constructor and the Bar static constructor both have to ensure that the Frob static constructor is called? In your scenario, the one that calls it second is sure to throw an exception.
You could potentially move that responsibility to the main program so that it calls the static constructor for Frob before calling the static constructors for Foo and Bar, but then every class's static constructor would have to be public, and your code would be full of calls to static constructors for things you know absolutely nothing about except that some class you care about needs it.
That doesn't sound like a very friendly design to me.
If your static constructor needs a parameter, put that parameter in the application configuration file or get it from the command line. I strongly recommend NOT creating a separate Initialize method that you call manually, mostly because it's difficult to ensure that such a thing is called when it's needed. See Be careful with static classes for a little bit more information, including some informative links.
So in my opinion the reason static constructors work the way they do is because that's the only way that makes sense, given the overall design of the language.
If what you're looking for is a singleton ... well, that's a different matter. I'd suggest Jon Skeet's Implementing the Singleton Pattern in C#.