Question on operator 'new' in c#.

A

Andrea

I found in internet this example of code. I'd like to know somthing about
what happens behind the scenes when these lines of code are executed:

//----------- Code Snippet ----------------------------

GenericCustomer[] Customers = new GenericCustomer[2];

Customers[0] = a;

Customers[1] = b;

//------------End Of Code Snippet-------------------------------------------

If i would be in a C/C++ environment this should be a dangling reference,
but since i've a garbage collector, what happen to the two objects created
with "new[2]", are they replaced by the two objects or are they passed to
the Garbage Collector to determine they life-time...?

//--------------------------------------Complete
Example---------------------------

public abstract class GenericCustomer {

private string name;

protected decimal balance;

public override string toString() {

string Result = "Customer: "+name;

Result+= ", owing: "+balance;

return Result;

}


public string Name {

get {

return name;

}

set {

name = value;

}

}


public decimal Balance {

get {

return balance;

}

}


public void RecordPayment(decimal amountPaid) {

balance += amountPaid;

}


public abstract void RecordCal(uint nMinutes)

}

public class PayAsYouGoCustomer : GenericCustomer {

public override void RecordCall(uint nMinutes) {

balance += nMinutes*0.5;

}

}

public class GoldCustomer : GenericCustomer {

public override void RecordCall(uint nMinutes) {

balance += nMinutes*0.1;

}

}

public class EntryPoint {

public static void Main() {

GenericCustomer a = new GoldCustomer();
GenericCustomer b = new PayAsYouGoCustomer();

a.name = "A";
b.name = "B";

GenericCustomer[] Customers = new GenericCustomer[2];

Customers[0] = a;
Customers[0].RecordCall(25);
Customers[0].RecordCall(75);
Customers[1] = b;
Customers[1].RecordCall(75);

foreach (GenericCustomer customer in Customers) {

Console.WriteLine("{0} owes {1}", customer.Name, customer.Balance);

}


}

}

//--------------------------------------End of Complete
Example---------------------------
 
K

Kevin Chandler

The new[2] does not create 2 GenericCustomer objects. This is a big
difference between c/c++ and c#. The new does nothing more than create the
array. The contents of the array are empty.

In c#, if you want to create an array and fill it with object (like what
c/c++ does), you need to assign each element in the array to a new
GenericCustomer.

I hope I understood your question and that this helps,
Kevin

Andrea said:
I found in internet this example of code. I'd like to know somthing about
what happens behind the scenes when these lines of code are executed:

//----------- Code Snippet ----------------------------

GenericCustomer[] Customers = new GenericCustomer[2];

Customers[0] = a;

Customers[1] = b;

//------------End Of Code
Snippet-------------------------------------------
 
R

Rob Windsor

Hi Andrea,

Your basically right. The two GenericCustomer objects created in the first
line will no longer have references to them after the third line completes
and thus they will be destroyed the next time the garbage collector runs. It
should be noted that unless you explicitly request a garbage collection
there is no way to know when the next collection will occur, it may not
happen until just before the application terminates.
 
F

Frank Oquendo

Thus spake Andrea:
GenericCustomer[] Customers = new GenericCustomer[2];
If i would be in a C/C++ environment this should be a dangling
reference, but since i've a garbage collector, what happen to the two
objects created with "new[2]", are they replaced by the two objects
or are they passed to the Garbage Collector to determine they
life-time...?

A simple test will reveal that you have an array of null references.
 
1

100

Rob Windsor said:
Hi Andrea,

Your basically right. The two GenericCustomer objects created in the first
line will no longer have references to them after the third line completes
and thus they will be destroyed the next time the garbage collector runs. It
should be noted that unless you explicitly request a garbage collection
there is no way to know when the next collection will occur, it may not
happen until just before the application terminates.

Hi Rob,
Speaking of C# the first line creates only one object and it is of type
derived from System.Array. There is no GenericCustomer objects created. To
be more strict GenericCustomer objects will be created if GenericCustomer is
a vaue type. In the last case these value-type objects are not considered
for GC, though.

This is the big difference between C/C++ and C# arrays.

B\rgds
100
Andrea said:
I found in internet this example of code. I'd like to know somthing about
what happens behind the scenes when these lines of code are executed:

//----------- Code Snippet ----------------------------

GenericCustomer[] Customers = new GenericCustomer[2];

Customers[0] = a;

Customers[1] = b;

//------------End Of Code Snippet-------------------------------------------

If i would be in a C/C++ environment this should be a dangling reference,
but since i've a garbage collector, what happen to the two objects created
with "new[2]", are they replaced by the two objects or are they passed to
the Garbage Collector to determine they life-time...?

//--------------------------------------Complete
Example---------------------------

public abstract class GenericCustomer {

private string name;

protected decimal balance;

public override string toString() {

string Result = "Customer: "+name;

Result+= ", owing: "+balance;

return Result;

}


public string Name {

get {

return name;

}

set {

name = value;

}

}


public decimal Balance {

get {

return balance;

}

}


public void RecordPayment(decimal amountPaid) {

balance += amountPaid;

}


public abstract void RecordCal(uint nMinutes)

}

public class PayAsYouGoCustomer : GenericCustomer {

public override void RecordCall(uint nMinutes) {

balance += nMinutes*0.5;

}

}

public class GoldCustomer : GenericCustomer {

public override void RecordCall(uint nMinutes) {

balance += nMinutes*0.1;

}

}

public class EntryPoint {

public static void Main() {

GenericCustomer a = new GoldCustomer();
GenericCustomer b = new PayAsYouGoCustomer();

a.name = "A";
b.name = "B";

GenericCustomer[] Customers = new GenericCustomer[2];

Customers[0] = a;
Customers[0].RecordCall(25);
Customers[0].RecordCall(75);
Customers[1] = b;
Customers[1].RecordCall(75);

foreach (GenericCustomer customer in Customers) {

Console.WriteLine("{0} owes {1}", customer.Name, customer.Balance);

}


}

}

//--------------------------------------End of Complete
Example---------------------------
 
A

Andrea

thank you all.
How do you do this memory test?


Frank Oquendo said:
Thus spake Andrea:
GenericCustomer[] Customers = new GenericCustomer[2];
If i would be in a C/C++ environment this should be a dangling
reference, but since i've a garbage collector, what happen to the two
objects created with "new[2]", are they replaced by the two objects
or are they passed to the Garbage Collector to determine they
life-time...?

A simple test will reveal that you have an array of null references.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
 
F

Frank Oquendo

Thus spake Andrea:
thank you all.
How do you do this memory test?

Here's some code. Substitute your GenericCustomer class to make the test
more relevant to your scenario:

using System;

namespace ConsoleApplication3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
object[] objects = new object[2];
string temp;

for (int i = 0; i < objects.Length; i++)
{
temp = objects == null ? "null" : "instantiated";
Console.WriteLine("Object {0} is {1}", i, temp);
}

Console.ReadLine();
}
}
}
 
A

Andrea

thanks again.

Frank Oquendo said:
Thus spake Andrea:
thank you all.
How do you do this memory test?

Here's some code. Substitute your GenericCustomer class to make the test
more relevant to your scenario:

using System;

namespace ConsoleApplication3
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
object[] objects = new object[2];
string temp;

for (int i = 0; i < objects.Length; i++)
{
temp = objects == null ? "null" : "instantiated";
Console.WriteLine("Object {0} is {1}", i, temp);
}

Console.ReadLine();
}
}
}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
 

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