Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Questions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
count1986@gmail.com - 21 Sep 2007 19:54 GMT
Please anybody explain me (C#):
1. Why default constructors of structure types cannot be defined?
2. Why finalize method cannot be overrided in structure types?
3. Why structure type cannot be used as a base type?
Jon Skeet [C# MVP] - 21 Sep 2007 20:18 GMT
> Please anybody explain me (C#):
> 1. Why default constructors of structure types cannot be defined?

Because they wouldn't always be called. For instance:

MyStruct[] x = new MyStruct[100];

Would you want that to call the parameterless constructor 100 times? It
would be a bit of a waste if you were about to overwrite all the
values.

What about just:

MyStruct y;

?

I seem to remember that there are some much odder cases, too. You could
say that the constructor would only *sometimes* be called, of course -
but that would limit their usefulness significantly.

> 2. Why finalize method cannot be overrided in structure types?

Structures aren't garbage collected unless they're boxed. The space is
just freed.

> 3. Why structure type cannot be used as a base type?

You'd get some odd (well, for some people) behaviour if you allowed
that. Consider the following code:

struct Base
{
   int x;
}

struct Derived : Base
{
   int y;
}

void SomeMethod()
{
   Base b = new Derived();
}

How much space should be allocated on the stack for the variable b?
Base only takes up 4 bytes, but Derived takes up 8. You'd have to
either disallow this, or only *actually* use a Base even though you've
asked for a Derived.

There *are* lots of options around here (as native C++ handles this
case, of course), but .NET makes things arguably simpler by disallowing
it in the first place.

I get the feeling Ben Voigt is going to reply to this, if he reads it
:)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

count1986@gmail.com - 21 Sep 2007 20:54 GMT
Laura T. - 21 Sep 2007 21:00 GMT
>> 3. Why structure type cannot be used as a base type?
>
[quoted text clipped - 20 lines]
> either disallow this, or only *actually* use a Base even though you've
> asked for a Derived.

Actually, your example could be read as C++ union datatype. Derived is a
union of base and derived.
Syntax is nice clean altough a bit strange at first sight. It could work.
:-)

>> Please anybody explain me (C#):
>> 1. Why default constructors of structure types cannot be defined?
[quoted text clipped - 53 lines]
> I get the feeling Ben Voigt is going to reply to this, if he reads it
> :)
Jon Skeet [C# MVP] - 21 Sep 2007 21:32 GMT
> Actually, your example could be read as C++ union datatype. Derived is a
> union of base and derived.
> Syntax is nice clean altough a bit strange at first sight. It could work.
> :-)

From what I remember, a union uses two variables for a single area of
storage. In my example, I'd expect x and y to be *independent*
variables.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.