Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi..

i want small confusion in C#.net Constructors and Destructors can override or not and also overloading are not please tell me and give me some small example of both solution please,..............


Thanking you.............
Posted

Overloading of constructors can be done by varying parameter signature just like any other method overloading.

An Intro to Constructors in C#[^]

However overriding of constructors is not supported ib C# or .NET since the purpose of the constructor is to initialize the members of class it is contained in, there is no need to override it to overide the functionality of the constructor in the base class.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Apr-12 12:55pm    
I voted 4, for the following reason. Everything is correct but the rational behind the lack of constructor override.
This is a typical case where the rational is adapted to an existing fact.

The real situation is different. Delphi, the C# predecessor allowed for virtual constructors, inheritance is static classes and virtual static method; all those features are of great use; but in .NET some of those features are replaced with Reflection (which is actually more powerful but was pretty much poorly documented in Pascal). So, there is a big need in overriding of constructors for some advanced uses, but the voluntary solution was made to keep it simpler and a bit closer to C++. The reason might be more complex and culture-dependent. Your explanation pretends to be rational, but it is not, which is proven by the fact that alternative approaches exist and work.

--SA
bbirajdar 12-Apr-12 0:21am    
Thank you SA for correcting me again. :) However, I am still confused about the concept of overriding of constructors. I will research on this concept (for my self improvement) and will try to update my solution.

The other thing to note is that Visual Studio gives a error something like ' 'override' modifier is not valid in the current context' if we apply a override keyword to constructor in a class. The message is not enough explanatory.
Information about destructors in .Net[^]; you can't override or overload a destructor (but you can specify one in any class which will be called, so it's kind of an override of ~Object).

Constructors can be overloaded like any method, but it doesn't make sense to override them as they're associated with a particular new ...() invocation and the class you put there specifies whose constructor should be called.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Apr-12 12:57pm    
I voted 4, for the following reason. Everything is correct but the rational behind the lack of constructor override. This is a typical case where the rational is adapted to an existing fact.

Please see my comment to Solution 2 where I explain it. Your "make no sense" is not a valid explanation. The fact is that there would be a full sense in it, but you did not think it and took some concrete decision for a fact of nature, which is not.

--SA
BobJanova 11-Apr-12 17:13pm    
Virtual constructors aren't virtual in the same sense as normal methods, even in Delphi; they are called on a type reference (in .Net speak, a Type) instead of on an instance. Constructors can be invoked in a similar way in ActionScript, and in .Net Activator.CreateInstance does a similar job.

In a way all constructors in .Net are already virtual and overridden, as you must call a base class constructor.
Sergey Alexandrovich Kryukov 12-Apr-12 11:15am    
Very good point, thank you for these notes.

This way, Delphi meta-types and virtual constructors and static methods are related. Meta-type in Delphi are more powerful, which make "type of T" possible. (I am really sorry that .NET does not provide fully-fledged meta-types as in Delphi, but fully standardized; I cannot explain why; from my perspective, it could be all feasible.) The possibility of virtual static methods is based on the simple fact: the meta-data + virtual method tables exist before any instance of the class is created; this meta-data is stored in some memory pertaining to the class itself and is created per class, not per instance, so the virtual static methods are quite possible. And the constructor can be considered as one of such method, specialized to create an instance.

I agree with you that the nature of Delphi constructor is different -- it is something like a static factory method, with possibility to be virtual.

I also agree that there is a functionally similar .NET implementation of activation, but -- not so powerful as in Delphi. I can tell you one maybe interesting thing about activation I recently discovered, related to performance. CreateInstance is too slow, and simple minimal ConstructorInfo.Invoke is too slow (considering its instance is created only once and its time is not counted, only the Invoke is called repetitively). In my attempt to optimize I found that System.Reflection.Emit of the code bound to a concrete type provides really, really dramatic improvement in performance (I am planning to time overall performance pretty soon). In this case, you can make some collection of emitted code to become a kind of the polymorphous "virtual constructor factory" which can combine a kind of late binding in object construction with the performance of direct call of the constructor.

Interesting, isn't it? It could take a whole article. For now, it could suggest that the combination of the flexibility of Delphi virtual constructors with Delphi performance presents not so trivial problem, if you want to implement something functionally equivalent with .NET. It also explains why Microsoft serializers use runtime-emitted assemblies -- the performance gain is dramatic, and the cost of dynamically dispatched constructor call is only one part of it.

--SA
BobJanova 12-Apr-12 13:11pm    
Virtual statics can be useful, certainly. And you are correct, in my mind, to say that a Delphi constructor is more like a (virtual) static method than a conventional language constructor, although it does have special things about it.

Delphi's meta-types are excellent, and even dynamic typing like JavaScript and ActionScript give you a lot more in this department than C#. I too would like to see that particularly idea stolen for .Net.

I don't see any reason why we can't have virtual statics, either.
Sergey Alexandrovich Kryukov 12-Apr-12 15:42pm    
Thank you for this interesting discussion, Bob.
--SA
Start here[^].
 
Share this answer
 
Comments
Lakamraju Raghuram 11-Apr-12 12:03pm    
Hmm..Creative
I like it
Sergey Alexandrovich Kryukov 11-Apr-12 12:58pm    
It addresses only a part of the question...
As to the overriding (virtual constructors), please see my comment to other solutions -- you might find them interesting.
--SA
Lakamraju Raghuram 11-Apr-12 13:30pm    
I think Mark merrens is prompting the OP to Google before posting here.
And yes it deals with only part the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900