About Struct in CSharp

  • Thread starter Thread starter Jamil Ahmed
  • Start date Start date
J

Jamil Ahmed

I have two questions about Structure in CSharp:

1. Why we can not override the Default Constructor in Structure.

2. Why we can not initialize the Fields in Structure.
 
There are some interop situations where the runtime could not guarantee that
such a constructor would be run, and therefore the language prohibits you
from writing one.

Initializing fields is the same issue, as it is done as part of the
constructor.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
structs are handled differently than classes in C#. The runtime will
automatically create default constructors that initialize your fields
to zero, false or null. This allows for quicker allocation of structs,
then classes. If you need default constructors or initializers, then
use a class.

Randy
http://www.kbcafe.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top