Re: Why can't value types have initializers?
- From: Pavel Minaev <int19h@xxxxxxxxx>
- Date: Tue, 31 Mar 2009 10:29:27 -0700 (PDT)
On Mar 31, 4:42 am, "Patrice" <http://www.chez.com/scribe/> wrote:
And for instance initializers, it looks like this is for interop
compatiblity :
http://bytes.com/groups/net-c/228739-about-struct-csharp
That's not what the link says. It says that structs cannot have
initializers because they cannot have "default constructors", and they
cannot have those because then the runtime would have to execute them
whenever a struct is allocated. Which is something that CLR,
apparently, doesn't do. To put it simply, right now, when you write
"new Point[100]", you get 100 zero-filled points because the runtime
simply allocates a block of memory large enough, and zero-initializes
it (actually, probably the OS does the latter, not even CLR). But if
Point could have a default constructor, then the runtime would have to
execute that constructor for all 100 Point objects in the new array,
similar to how this happens in C++.
Now, this is certainly not impossible to do; but C# isn't C++ for a
reason. C++ rules for when constructors and destructors run can get
rather complicated at times (consider interaction with function
arguments, class members, exceptions etc).
.
- Follow-Ups:
- Re: Why can't value types have initializers?
- From: Patrice
- Re: Why can't value types have initializers?
- References:
- Why can't value types have initializers?
- From: ....DotNet4Ever....
- Re: Why can't value types have initializers?
- From: Patrice
- Why can't value types have initializers?
- Prev by Date: Problems with flashplayer in ie and the arrowkeys
- Next by Date: Re: Why can't value types have initializers?
- Previous by thread: Re: Why can't value types have initializers?
- Next by thread: Re: Why can't value types have initializers?
- Index(es):
Relevant Pages
|