> From: "Richard Blewett" <[EMAIL PROTECTED]>
>
> struct Point
> {
> public int X;
> public int Y;
> }
>
> I can then write:
>
> Point p;
> p.X = 10;
> p.Y = 20;
>
> The declaration "gets me a new instance of this type". All new does for a
> value type is give me a hook on which to hang my "run this particular
> constructor" coat.
In addition the use of new causes the compiler (at least for C#) to consider
a value type to be initialized.
Consider the following:
Point p1 = new Point(); // p1.X and p1.Y are initialized to 0.
Point p2; // p2.X and p2.Y are initialized to 0.
int x1 = p1.X; // Ok, as we used new teh compiler considers the field
intialized.
int x2 = p2.X; // error CS0170: Use of possibly unassigned field 'X'
Both p1 and p2 are allocated on the stack and their fields are initialized
to 0, but if we do not use the new keyword we have to initialize the fields
ourselves before we read their values.
/Joakim
http://www.jkarlsson.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com
Some .NET courses you may be interested in:
NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls
View archives and manage your subscription(s) at http://discuss.develop.com