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