Object instantiation in C#

G

Guest

I come form a C++ background and am new to C#. I am puzzled by the object
instantiation mechanism, and would be grateful if someone could clarify the
following related questions:
- If I declare a property as a user-defined object (ie as a reference) how
can I test to see if it has been instantiated (assigned) or not?
- If I use 'new' to instantiate an object, but the reference variable I use
already refers to an instantiation of the class, is the reference variable
reassigned, and if so what happens to the object it previously referenced
(which is now presumably completely inaccessible)?
 
G

Guest

- If I declare a property as a user-defined object (ie as a reference) how
can I test to see if it has been instantiated (assigned) or not?

Check if it is null. All objects in C# default to their default values if
you don't explicitly instantiate/initialize them. The default value for
reference types is null.

e.g.

MyObject obj;

if(obj == null)
{
obj = new MyObject();
}
- If I use 'new' to instantiate an object, but the reference variable I use
already refers to an instantiation of the class, is the reference variable
reassigned, and if so what happens to the object it previously referenced
(which is now presumably completely inaccessible)?

I assume you mean something like this:

MyObject object1 = new MyObject();

object1 = new MyObject(); // Reassigning it.

Basically object1 now points to a new object. The old object will be garbage
collected.

Hope this helps.

Ah, you might find this C# for C++ programmers FAQ useful:
http://www.andymcm.com/csharpfaq.htm

--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
 
O

Oliver Sturm

Dave said:
I come form a C++ background and am new to C#. I am puzzled by the object
instantiation mechanism, and would be grateful if someone could clarify the
following related questions:
- If I declare a property as a user-defined object (ie as a reference) how
can I test to see if it has been instantiated (assigned) or not?

If you have

MyObject obj;

as a field in a class, it will be initialized automatically with null.
So you can check if it's still null:

if (obj == null)

If you have such a variable in a method, you need to initialize it
(either with null or with an instance of MyObject) before referring to
it (the compiler checks that for you, so you can't go wrong).

- If I use 'new' to instantiate an object, but the reference variable I use
already refers to an instantiation of the class, is the reference variable
reassigned, and if so what happens to the object it previously referenced
(which is now presumably completely inaccessible)?

MyObject obj = new MyObject();
obj = new MyObject();

That's what you mean, right? In this case, the first instance of
MyObject that you created is no longer accessible and will be garbage
collected some time in the future. The bottom line is, you don't have to
care about it in any way. The longer version is that there's a lot of
documentation all around about how exactly the garbage collector works
and what can be done if a specific behaviour would be preferred - but I
suggest you leave that until later :)

Hope this helps!


Oliver Sturm
 
G

Guest

I shoudl actually qualify what I said in the previous response:

"All objects in C# default to their default values if
you don't explicitly instantiate/initialize them. The default value for
reference types is null."

This is only true for class/object level variables/fields (whatever you want
to call them :). Local variables do not default to any value.... UNLESS they
are in an array, in which case they are assigned their default value.

This will explain it better:

public class MyClass
{
private string helloString; // Defaults to null

public void SomeMethod()
{
if(helloString == null) // evaluates to true.
{
helloString = "hello!"; // = is overloaded
}

string someString; // is not instantiated as
// it is a local variable

if(someString == null) // compiler error
{
}

string[] stringArray = new string[20];

if(stringArray[1] == null) // Perfectly fine
{
// Do Something
}

}
}

Hope this helps.

--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
 
G

Guest

Thanks for that. I assumed it was somthing like that. I am using O'Reilly
"Programming C#" by Jesse Liberty and I can't find any reference to 'null' -
maybe I'm going blind. Anyway, now I know, thanks.
--
Dave


Brian Delahunty said:
I shoudl actually qualify what I said in the previous response:

"All objects in C# default to their default values if
you don't explicitly instantiate/initialize them. The default value for
reference types is null."

This is only true for class/object level variables/fields (whatever you want
to call them :). Local variables do not default to any value.... UNLESS they
are in an array, in which case they are assigned their default value.

This will explain it better:

public class MyClass
{
private string helloString; // Defaults to null

public void SomeMethod()
{
if(helloString == null) // evaluates to true.
{
helloString = "hello!"; // = is overloaded
}

string someString; // is not instantiated as
// it is a local variable

if(someString == null) // compiler error
{
}

string[] stringArray = new string[20];

if(stringArray[1] == null) // Perfectly fine
{
// Do Something
}

}
}

Hope this helps.

--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.


Dave said:
I come form a C++ background and am new to C#. I am puzzled by the object
instantiation mechanism, and would be grateful if someone could clarify the
following related questions:
- If I declare a property as a user-defined object (ie as a reference) how
can I test to see if it has been instantiated (assigned) or not?
- If I use 'new' to instantiate an object, but the reference variable I use
already refers to an instantiation of the class, is the reference variable
reassigned, and if so what happens to the object it previously referenced
(which is now presumably completely inaccessible)?
 
G

Guest

Thanks Oliver. See my reply to Brian.
--
Dave


Oliver Sturm said:
If you have

MyObject obj;

as a field in a class, it will be initialized automatically with null.
So you can check if it's still null:

if (obj == null)

If you have such a variable in a method, you need to initialize it
(either with null or with an instance of MyObject) before referring to
it (the compiler checks that for you, so you can't go wrong).



MyObject obj = new MyObject();
obj = new MyObject();

That's what you mean, right? In this case, the first instance of
MyObject that you created is no longer accessible and will be garbage
collected some time in the future. The bottom line is, you don't have to
care about it in any way. The longer version is that there's a lot of
documentation all around about how exactly the garbage collector works
and what can be done if a specific behaviour would be preferred - but I
suggest you leave that until later :)

Hope this helps!


Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
G

Guest

Thanks for that.

No problem. Glad I could help.

Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.


Dave said:
Thanks for that. I assumed it was somthing like that. I am using O'Reilly
"Programming C#" by Jesse Liberty and I can't find any reference to 'null' -
maybe I'm going blind. Anyway, now I know, thanks.
--
Dave


Brian Delahunty said:
I shoudl actually qualify what I said in the previous response:

"All objects in C# default to their default values if
you don't explicitly instantiate/initialize them. The default value for
reference types is null."

This is only true for class/object level variables/fields (whatever you want
to call them :). Local variables do not default to any value.... UNLESS they
are in an array, in which case they are assigned their default value.

This will explain it better:

public class MyClass
{
private string helloString; // Defaults to null

public void SomeMethod()
{
if(helloString == null) // evaluates to true.
{
helloString = "hello!"; // = is overloaded
}

string someString; // is not instantiated as
// it is a local variable

if(someString == null) // compiler error
{
}

string[] stringArray = new string[20];

if(stringArray[1] == null) // Perfectly fine
{
// Do Something
}

}
}

Hope this helps.

--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.


Dave said:
I come form a C++ background and am new to C#. I am puzzled by the object
instantiation mechanism, and would be grateful if someone could clarify the
following related questions:
- If I declare a property as a user-defined object (ie as a reference) how
can I test to see if it has been instantiated (assigned) or not?
- If I use 'new' to instantiate an object, but the reference variable I use
already refers to an instantiation of the class, is the reference variable
reassigned, and if so what happens to the object it previously referenced
(which is now presumably completely inaccessible)?
 
B

Bill Butler

Dave said:
I come form a C++ background and am new to C#. I am puzzled by the object
instantiation mechanism, and would be grateful if someone could clarify the
following related questions:
- If I declare a property as a user-defined object (ie as a reference) how
can I test to see if it has been instantiated (assigned) or not?
- If I use 'new' to instantiate an object, but the reference variable I use
already refers to an instantiation of the class, is the reference variable
reassigned, and if so what happens to the object it previously referenced
(which is now presumably completely inaccessible)?

Hi Dave,

The other posters answered you question, but let me go a step further.

Example 1:
A a = new A("Foo"); // a is a reference to an instance of A that we will call Foo
a = new A("Bar"); // a is now a reference to an instance of A that we will call Bar
// Foo is eligible for garbage collection


Example 2:
A a = new A("Foo"); // a is a reference to Foo
B b = new B("qwerty"); // b is a reference to an instance of B that we will call qwerty
b.X = a; // X is a property of type A
a = new A("Bar"); // a is now a reference to Bar
// Foo is NOT eligible for garbage collection
// since qwerty still has a reference to FOO

Example 3:
A a = new A("Foo"); // a is a reference to Foo
B b = new B("qwerty"); // b is a reference to qwerty
b.X = a; // X is a property of type A
a = new A("Bar"); // a is now a reference to Bar
b = null; // there is no reference to qwerty
// Foo is eligible for garbage collection
// even though qwerty still has a reference to Foo
// qwerty is also eligible for garbage collection

Example 4:
A a = new A("Foo"); // a is a reference to Foo
B b = new B("qwerty"); // b is a reference to qwerty
b.X = a; // X is a property of type A
a.Y = b; // Y is a property of type B
a = null // a now refers to nothing
b = null; // b now refers to nothing
// Foo and qwerty are both eligible for GC
// even though they both refer to each other.
// This is because there is no "Reachable" reference to either one.

Hope this helps
Bill
 
G

Guest

That's a great answer, thanks. I can see this is going to be the headache
area for C++ migrants.

I have another question, I was going to make a new post, but it's sort of
related:

I have a class MyClass that throws exceptions. It could throw one in the
constructor. If it throws I want to call another method on it (maybe request
status)

MyMain()
{
MyClass myObject;
try
{
myObject = new MyClass(...) //might throw here
// do stuff
}
catch (MyException exception)
{
myObject.GetStatus() // compiler complains - unassigned local
variable
}
myObject.GetStatus() // compiler complains here too
}

If I declare it inside the try block that fails too because it's not even
declared in the catch block.
 
B

Bill Butler

I have a class MyClass that throws exceptions. It could throw one in the
constructor. If it throws I want to call another method on it (maybe
request
status)

When an UNCAUGHT exception occurs in a constructor, the object is not
created.
Also the assignment never takes place ,so your reference will still be
pointing to whatever it was pointing to before.

Lets use this class for these examples:

class Bad
{
public int X;
public Bad(int x)
{
if (x > 3)
throw new Exception("(x > 3)");
X = x;
}
}
-------------------------------------------------------
Example 1: // similar to your example
public static void Test1()
{
Bad bad = null; // This was what the compiler was complaining
about
try
{
bad = new Bad(5);
}
catch (Exception ex)
{
if (bad == null)
Console.WriteLine("(bad == null)");
else
Console.WriteLine("bad.X = {0}", bad.X);
}
}
-------------------------------------
Output:
(bad == null)
=====================================================
Example 2: // Add a loop

public static void Test2()
{
Bad bad = null; // This was what the compiler was complaining
about

for (int i = 0; i < 10; i++)
{
try
{
bad = new Bad(i);
}
catch (Exception ex)
{
if (bad == null)
Console.WriteLine("(bad == null)");
else
Console.WriteLine("bad.X = {0}", bad.X);
}
}
}
-------------------------------------
Output:
bad.X = 3
bad.X = 3
bad.X = 3
bad.X = 3
bad.X = 3
bad.X = 3
==========================================================


Hope this Helps
Bill
 
G

Guest

Aha - got it. I never thought of initialising to null. As I think I've
mentioned before, Jesse Liberty's book doesn't seem to cover null at all.
Many thanks for that.
 
B

Bill Butler

Dave said:
Aha - got it. I never thought of initialising to null. As I think I've
mentioned before, Jesse Liberty's book doesn't seem to cover null at all.
Many thanks for that.
--


No problem
 
J

Jon Skeet

When an UNCAUGHT exception occurs in a constructor, the object is not
created.

No, that's not actually correct. The object is created before the
constructor is ever run, and if the constructor stashes a reference to
itself (eg in a static variable) before the exception is thrown, that object
will hang around as normal.

In *most* cases, it is *as if* the object has never been created, because
the object usually becomes immediately eligible for garbage collection. It's
worth being aware of what's actually happening though.
 
B

Bill Butler

Jon Skeet said:
On 12/8/05 3:44 pm, in article #[email protected],
"Bill

No, that's not actually correct. The object is created before the
constructor is ever run, and if the constructor stashes a reference to
itself (eg in a static variable) before the exception is thrown, that
object
will hang around as normal.

In *most* cases, it is *as if* the object has never been created, because
the object usually becomes immediately eligible for garbage collection.
It's
worth being aware of what's actually happening though.

Thanks for the clarification.
I guess extreme care should be taken when passing "this" in a constructor.
Using partially created objects is not a good idea in general.

Thanks
Bill
 
Q

Questman

Dave said:
I come form a C++ background and am new to C#. I am puzzled by the object
instantiation mechanism, and would be grateful if someone could clarify the
following related questions:
- If I declare a property as a user-defined object (ie as a reference) how
can I test to see if it has been instantiated (assigned) or not?
- If I use 'new' to instantiate an object, but the reference variable I use
already refers to an instantiation of the class, is the reference variable
reassigned, and if so what happens to the object it previously referenced
(which is now presumably completely inaccessible)?

Dave -

You wouldn't happen to be the same Dave M. who wrote a few C-based BBS
games for the Major BBS platform in the 1989-1993 timeframe, are you?

Rick
 
G

Guest

No. I'm not.
--
Dave


Questman said:
Dave -

You wouldn't happen to be the same Dave M. who wrote a few C-based BBS
games for the Major BBS platform in the 1989-1993 timeframe, are you?

Rick
 
M

Mabden

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on Usenet and in email?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top