473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

who to know if c# creates object or not

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

Nov 15 '05 #1
6 2076

"Mikael Janers" <NO************@home.se__SPAMFILTER> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
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.

--
Iain Simpson
Nov 15 '05 #2
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.

"Mikael Janers" <NO************@home.se__SPAMFILTER> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
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


Nov 15 '05 #3
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" <NO************@home.se__SPAMFILTER> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
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


Nov 15 '05 #4
Mikael Janers <NO************@home.se__SPAMFILTER> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
Wow, this group is realy fast =)

Thank you all for the help!

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Mikael Janers <NO************@home.se__SPAMFILTER> wrote:
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).

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

Nov 15 '05 #6
100
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
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Chris Lasher | last post by:
Hello, I really like the finditer() method of the re module. I'm having difficulty at the moment, however, because finditer() still creates a callable-iterator oject, even when no match is found....
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
3
by: Jim Frazer | last post by:
Hi, I'm new to C#, but experienced in C++. I'm converting an ActiveX object to C#. There is a single instance of a "primary" class which creates two collections of other "secondary" classes. ...
8
by: Phill | last post by:
Anyone know the internal working of this thing. I expected it to be kinda slow at enumerating large lists of objects but it was actually pretty good. I was able to write a quick linked list class...
5
by: maya | last post by:
Hi, I'm writing an application that gets a string as an input from the user, and creates an image with this string. The library that creates the image doesn't recognize if the string is a...
7
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
1
by: kaczmar2 | last post by:
I have an ASP.NET page where controls are created dynamically, and I have an issue where one event handler creates another set of controls, and then adds event handlers to those controls. The...
3
by: active | last post by:
I'm in the PictureBox click event and I need to know which button was pressed. I'm sure I once saw a property in Form or SystemInformation or someplace that system wide that enabled one to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.