Dear All,
We all know static condtructors in C# are used to initialize the static data members within a class.
But it cannot be overloaded.
My question is that why this feature is not supported?
My understanding::
Is it just because that we cannot associate an object with the static constructor like the way we do it in case of non static ones by explicitly calling them while crating an instance of the class. See the following
ClassEmployee objClassEmployee = new objClassEmployee("Emp001","Upamanyu",30000); // explicitly calling the overloaded contrcutor
But we cannot do like that
StaticCons();
StaticCons(("Emp001","Upamanyu",30000); // we do not call static construtors, it gets called before the first instance of the class gets referenced.
So the overloaded feature is off in case of static ones???
Please provide your views on it.
Many Thanks,
Upamanyu
Loading


VulpesRank #55 · 29.2K reputationPosted Feb 11, 2013, 2:11 PM EST
Consequently, it makes no sense for it to take any parameters as the system would have no way of knowing what arguments to pass for those parameters.
As it can't take any parameters, it follows that it can't be overloaded either.
Upamanyu Roy Choudhury146 reputationPosted Feb 12, 2013, 11:07 PM EST
We can call a static method like this
ClassName.StaticMethod
Then why not constructors?? Why MS has not given this fecility???
VulpesRank #55 · 29.2K reputationPosted Feb 13, 2013, 12:26 AM EST
Upamanyu Roy Choudhury146 reputationPosted Feb 23, 2013, 9:21 AM EST
I can get your point but where can we find what are going on behind the scene.
It is true that everything we cannot know by doing RND but atleast I am trying to get some in depth knowledge about the internals.
Upamanyu Roy Choudhury146 reputationPosted Feb 23, 2013, 9:25 AM EST
VulpesRank #55 · 29.2K reputationPosted Feb 24, 2013, 5:24 AM EST
http://www.amazon.com/CLR-via-Microsoft-Developer-Reference/sim/0735667454/2?o=18&po=9
Static constructors certainly have their problems such as deciding which one is called first when they include code which calls members of another class.
It's also not a good idea to put too much code into them as this can create a noticeable delay when accessing a member of the containing class for the first time.