Hmmm ...

Surely its not *quite* that simple for a) as of course these references are
themselves strongly typed - i.e. I cannot assign a Foo object to a Bar
reference if there is no type relationship between Foo and Bar. Thankfully
we don't have an equivalent of C++'s reinterpret_cast.

And with b), well for value types new doesn't mean "get me a new instance of
this type". Consider:

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.

And this is the problem. new in C++ and Java means allocate on the heap. For
people coming to C# from those worlds that will be their expectation -
overloading its use in the case of value types frequently causes confusion
for those new to C#. They assume that normally value types are allocated on
the stack but new causes them to be allocated on the heap.

It's also worth reiterating your point that value types aren't always
allocated on the stack. They are allocated inline with where they are
declared. As local variables they will be allocated on the stack but for
this class:

class Person
{
        public in Age;
}

The age will be allocated on the managed heap, inline with the rest of the
Person object.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Eric Gunnerson
Sent: 12 July 2004 18:59
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Value and Reference Types confusion.

Inline...

Eric
-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Ankit Bhurat
Sent: Monday, July 12, 2004 10:35 AM
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] Value and Reference Types confusion.

Hi all,

These days I am reading about .NET Framework. I came across Value and
Reference Types and developed some confusion. Pls clarify :

a)       One book said that all the objects of type Struct are of value
type
and all Class Objects are of reference types .
The same book said : If cTest is a class then

cTest x = new cTest();
cTest y ;

both x and y are variables of type reference.

<eric>

That is correct.

</eric>


b)       the other book said that all variables declared with "new" are
reference variable and are allocated memory on managed heap. And value
type
are allocated memory on stack.

<eric>

That's incorrect. While in C/C++, "new" means "allocate on the heap", in
C#, it just means "get me a new instance of this type". If it's a class,
it will be on the heap. If it's a struct, it will be on the stack (or
contained inside some other object).

</eric>

I have no confusion about boxing and unboxing .
Since I have read C/C++ , I guess the (b) point is more close to the
fact.

I am totally confused.

Regards,
Ankit


===================================
This list is hosted by DevelopMentor(r)  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

===================================
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

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.716 / Virus Database: 472 - Release Date: 05/07/2004


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.716 / Virus Database: 472 - Release Date: 05/07/2004

===================================
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

Reply via email to