who to know if c# creates object or not

  • Thread starter Thread starter Mikael Janers
  • Start date Start date
M

Mikael Janers

I've tried to find answer to this question on the net but without success,
so I hope someone here knows...

How can I know if c# actualy creates an instance of an object when I declare
a variable...

For example....

If I declare
private System.Collections.ArrayList m_aArray;

Then I dont have an instance of that object

But this declaretion

private System.Guid m_testId;

acctually results in an object..

I'm from the C++ school so this is very confusing to me, thanks for the
help!



// Mikael
 
If I declare
private System.Collections.ArrayList m_aArray;

Then I dont have an instance of that object

But this declaretion

private System.Guid m_testId;

acctually results in an object..

System.Guid is actually a value type. You are able to treat it like an object
because C# boxes it up for you automatically.
 
Basically if the class is derived from System.ValueType then declaring a
variable of that type will assign it an instance using the default
constructor (unless you specify otherwise).

System.Guid is a structure and thus derives from System.ValueType.

S.
 
Structs can't be derived from as they are always implicitly derived from
ValueType so it's pretty easy to tell by going to the definition or by
looking at the documentation which always postfixes the type ( class or
structure).


Regards
Lee
 
Mikael Janers said:
I've tried to find answer to this question on the net but without success,
so I hope someone here knows...

How can I know if c# actualy creates an instance of an object when I declare
a variable...

For example....

If I declare
private System.Collections.ArrayList m_aArray;

Then I dont have an instance of that object

But this declaretion

private System.Guid m_testId;

acctually results in an object..

I'm from the C++ school so this is very confusing to me, thanks for the
help!

Guid is a value type. ArrayList is a reference type. In both cases, you
get a variable which has the "default value": the default value of a
reference type is null (and no instances are created); the default
value of a value type is the value which has the default value for each
variable (0, null, false etc).
 
Hi Jon:
Guid is a value type. ArrayList is a reference type. In both cases, you
get a variable which has the "default value": the default value of a
reference type is null (and no instances are created); the default
value of a value type is the value which has the default value for each
variable (0, null, false etc).

Value type has default value only if it is created in the managed heap (as a
part of the other type). If you create a value type in the stack, though,
the value is not defined. C# compiler won't let you use not initialized
value type created in the thread stack

In the example given by Mikael:
private System.Guid m_testId;
m_testId obvioulsy is field of the other type. If it is field of a reference
type (class) it will have default value as long it is going to be created in
the managed heap. If it is field of a value type (struct) it might have or
it might have not a default value.

B\rgds
100
 
Back
Top