Question on operator 'new' in c#.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrea

    Question on operator 'new' in c#.

    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(d ecimal amountPaid) {

    balance += amountPaid;

    }


    public abstract void RecordCal(uint nMinutes)

    }

    public class PayAsYouGoCusto mer : 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 PayAsYouGoCusto mer();

    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 (GenericCustome r customer in Customers) {

    Console.WriteLi ne("{0} owes {1}", customer.Name, customer.Balanc e);

    }


    }

    }

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





  • Kevin Chandler

    #2
    Re: Question on operator 'new' in c#.

    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" <kyrenar@hotmai l.com> wrote in message
    news:eU$6tvbgDH A.3208@TK2MSFTN GP11.phx.gbl...[color=blue]
    > 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[/color]
    Snippet-------------------------------------------


    Comment

    • Rob Windsor

      #3
      Re: Question on operator 'new' in c#.

      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.

      --
      Rob Windsor
      G6 Consulting
      Toronto, Canada


      "Andrea" <kyrenar@hotmai l.com> wrote in message
      news:eU$6tvbgDH A.3208@TK2MSFTN GP11.phx.gbl...[color=blue]
      > 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[/color]
      Snippet-------------------------------------------[color=blue]
      >
      > 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(d ecimal amountPaid) {
      >
      > balance += amountPaid;
      >
      > }
      >
      >
      > public abstract void RecordCal(uint nMinutes)
      >
      > }
      >
      > public class PayAsYouGoCusto mer : 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 PayAsYouGoCusto mer();
      >
      > 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 (GenericCustome r customer in Customers) {
      >
      > Console.WriteLi ne("{0} owes {1}", customer.Name, customer.Balanc e);
      >
      > }
      >
      >
      > }
      >
      > }
      >
      > //--------------------------------------End of Complete
      > Example---------------------------
      >
      >
      >
      >
      >[/color]


      Comment

      • Frank Oquendo

        #4
        Re: Question on operator 'new' in c#.

        Thus spake Andrea:
        [color=blue]
        > GenericCustomer[] Customers = new GenericCustomer[2];[/color]
        <snip>[color=blue]
        > 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...?[/color]

        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.




        Comment

        • 100

          #5
          Re: Question on operator 'new' in c#.


          "Rob Windsor" <rwindsor@NO.MO RE.SPAM.bigfoot .com> wrote in message
          news:e3513JegDH A.3204@TK2MSFTN GP11.phx.gbl...[color=blue]
          > 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.[/color]
          It[color=blue]
          > 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.
          >
          > --
          > Rob Windsor
          > G6 Consulting
          > Toronto, Canada[/color]

          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
          [color=blue]
          >
          >
          > "Andrea" <kyrenar@hotmai l.com> wrote in message
          > news:eU$6tvbgDH A.3208@TK2MSFTN GP11.phx.gbl...[color=green]
          > > I found in internet this example of code. I'd like to know somthing[/color][/color]
          about[color=blue][color=green]
          > > 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[/color]
          > Snippet-------------------------------------------[color=green]
          > >
          > > If i would be in a C/C++ environment this should be a dangling[/color][/color]
          reference,[color=blue][color=green]
          > > but since i've a garbage collector, what happen to the two objects[/color][/color]
          created[color=blue][color=green]
          > > with "new[2]", are they replaced by the two objects or are they passed[/color][/color]
          to[color=blue][color=green]
          > > 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(d ecimal amountPaid) {
          > >
          > > balance += amountPaid;
          > >
          > > }
          > >
          > >
          > > public abstract void RecordCal(uint nMinutes)
          > >
          > > }
          > >
          > > public class PayAsYouGoCusto mer : 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 PayAsYouGoCusto mer();
          > >
          > > 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 (GenericCustome r customer in Customers) {
          > >
          > > Console.WriteLi ne("{0} owes {1}", customer.Name, customer.Balanc e);
          > >
          > > }
          > >
          > >
          > > }
          > >
          > > }
          > >
          > > //--------------------------------------End of Complete
          > > Example---------------------------
          > >
          > >
          > >
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Andrea

            #6
            Re: Question on operator 'new' in c#.

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


            "Frank Oquendo" <franko@acadx.c om> ha scritto nel messaggio
            news:uws6kUegDH A.1408@tk2msftn gp13.phx.gbl...[color=blue]
            > Thus spake Andrea:
            >[color=green]
            > > GenericCustomer[] Customers = new GenericCustomer[2];[/color]
            > <snip>[color=green]
            > > 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...?[/color]
            >
            > 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
            >
            >[/color]


            Comment

            • Frank Oquendo

              #7
              Re: Question on operator 'new' in c#.

              Thus spake Andrea:
              [color=blue]
              > thank you all.
              > How do you do this memory test?[/color]

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

              using System;

              namespace ConsoleApplicat ion3
              {
              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[i] == null ? "null" : "instantiat ed";
              Console.WriteLi ne("Object {0} is {1}", i, temp);
              }

              Console.ReadLin e();
              }
              }
              }

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




              Comment

              • Andrea

                #8
                Re: Question on operator 'new' in c#.

                thanks again.

                "Frank Oquendo" <franko@acadx.c om> ha scritto nel messaggio
                news:%23XuQpjeg DHA.2328@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                > Thus spake Andrea:
                >[color=green]
                > > thank you all.
                > > How do you do this memory test?[/color]
                >
                > Here's some code. Substitute your GenericCustomer class to make the test
                > more relevant to your scenario:
                >
                > using System;
                >
                > namespace ConsoleApplicat ion3
                > {
                > 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[i] == null ? "null" : "instantiat ed";
                > Console.WriteLi ne("Object {0} is {1}", i, temp);
                > }
                >
                > Console.ReadLin e();
                > }
                > }
                > }
                >
                > --
                > There are 10 kinds of people. Those who understand binary and those who
                > don't.
                >
                > http://code.acadx.com
                >
                >[/color]


                Comment

                • Jon Skeet

                  #9
                  Re: Question on operator 'new' in c#.

                  Andrea <kyrenar@hotmai l.com> wrote:[color=blue]
                  > How do you do this memory test?[/color]

                  if (Customers[0]==null)
                  ...

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  Working...