Problem about operator = in C#

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

    #1

    Problem about operator = in C#

    I defined my class as:
    class List
    {
    public List Next;
    public int Data;
    public List()
    {
    Data=0;
    }
    }
    ¡­¡­¡­¡­¡­¡­¡­¡ ­
    after the programme below ran,
    List d=new List();
    List e=new List();
    e.Data=4;
    d=e;
    d.Data=5;
    The value of e.Data became 5,What does it do?if I don't want the
    e.Data'value changed,what should i do? btw:the operator = can't be override
    in c#.Thank you


  • Peter Koen

    #2
    Re: Problem about operator = in C#

    "Archer" <xydarcher@etan g.com> wrote in
    news:Ow5aVB2mDH A.2964@tk2msftn gp13.phx.gbl:
    [color=blue]
    > I defined my class as:
    > class List
    > {
    > public List Next;
    > public int Data;
    > public List()
    > {
    > Data=0;
    > }
    > }
    > ¡­¡­¡­¡­¡­¡­¡­¡ ­
    > after the programme below ran,
    > List d=new List();
    > List e=new List();
    > e.Data=4;
    > d=e;
    > d.Data=5;
    > The value of e.Data became 5,What does it do?if I don't want the
    > e.Data'value changed,what should i do? btw:the operator = can't be
    > override in c#.Thank you
    >
    >
    >[/color]

    What about using System.Collecti ons.ArrayList instead of constructing your
    own List?

    And if you really want your own impl: a class is always passed by
    reference. so you will need a struct for your desired behaviour.

    --
    best regards

    Peter Koen
    -----------------------------------
    MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

    Comment

    • Archer

      #3
      Re: Problem about operator = in C#

      What about the object i created for e?
      is it be released auto?And what is the difference between stuct & class in
      C#?
      "Peter Koen" <koen-newsreply&snusn u.at> дÈëÓʼþ
      news:ezfZRF2mDH A.360@TK2MSFTNG P12.phx.gbl...[color=blue]
      > "Archer" <xydarcher@etan g.com> wrote in
      > news:Ow5aVB2mDH A.2964@tk2msftn gp13.phx.gbl:
      >[color=green]
      > > I defined my class as:
      > > class List
      > > {
      > > public List Next;
      > > public int Data;
      > > public List()
      > > {
      > > Data=0;
      > > }
      > > }
      > > ¡­¡­¡­¡­¡­¡­¡­¡ ­
      > > after the programme below ran,
      > > List d=new List();
      > > List e=new List();
      > > e.Data=4;
      > > d=e;
      > > d.Data=5;
      > > The value of e.Data became 5,What does it do?if I don't want the
      > > e.Data'value changed,what should i do? btw:the operator = can't be
      > > override in c#.Thank you
      > >
      > >
      > >[/color]
      >
      > What about using System.Collecti ons.ArrayList instead of constructing your
      > own List?
      >
      > And if you really want your own impl: a class is always passed by
      > reference. so you will need a struct for your desired behaviour.
      >
      > --
      > best regards
      >
      > Peter Koen
      > -----------------------------------
      > MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
      > http://www.kema.at[/color]


      Comment

      • Archer

        #4
        Re: Problem about operator = in C#

        What about the object i created for e?
        is it be released auto?And what is the difference between stuct & class in
        C#?
        "Peter Koen" <koen-newsreply&snusn u.at> дÈëÓʼþ
        news:ezfZRF2mDH A.360@TK2MSFTNG P12.phx.gbl...[color=blue]
        > "Archer" <xydarcher@etan g.com> wrote in
        > news:Ow5aVB2mDH A.2964@tk2msftn gp13.phx.gbl:
        >[color=green]
        > > I defined my class as:
        > > class List
        > > {
        > > public List Next;
        > > public int Data;
        > > public List()
        > > {
        > > Data=0;
        > > }
        > > }
        > > ¡­¡­¡­¡­¡­¡­¡­¡ ­
        > > after the programme below ran,
        > > List d=new List();
        > > List e=new List();
        > > e.Data=4;
        > > d=e;
        > > d.Data=5;
        > > The value of e.Data became 5,What does it do?if I don't want the
        > > e.Data'value changed,what should i do? btw:the operator = can't be
        > > override in c#.Thank you
        > >
        > >
        > >[/color]
        >
        > What about using System.Collecti ons.ArrayList instead of constructing your
        > own List?
        >
        > And if you really want your own impl: a class is always passed by
        > reference. so you will need a struct for your desired behaviour.
        >
        > --
        > best regards
        >
        > Peter Koen
        > -----------------------------------
        > MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
        > http://www.kema.at[/color]


        Comment

        • Peter Koen

          #5
          Re: Problem about operator = in C#

          "Archer" <xydarcher@etan g.com> wrote in
          news:#T#hXN2mDH A.2404@TK2MSFTN GP12.phx.gbl:
          [color=blue]
          > What about the object i created for e?
          > is it be released auto?And what is the difference between stuct &
          > class in C#?[/color]

          All objects will be released and collected by the garbage collector.

          structs are value types
          classes are reference types

          when you're asking such questionis it would be good if you read some basic
          tutorial before you start thinking about stuff like linked lists.

          http://msdn.microsoft.com/netframewo...d/default.aspx

          --
          best regards

          Peter Koen
          -----------------------------------
          MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

          Comment

          • David

            #6
            Re: Problem about operator = in C#

            In article <Ow5aVB2mDHA.29 64@tk2msftngp13 .phx.gbl>, Archer wrote:

            Let's look at the code step by step...
            [color=blue]
            > ¡­¡­¡­¡­¡­¡­¡­¡ ­
            > after the programme below ran,
            > List d=new List();[/color]
            // d is a reference to a new List object, we'll call the object List #1[color=blue]
            > List e=new List();[/color]
            // e is a reference to a new List object, let's call the object List #2[color=blue]
            > e.Data=4;[/color]
            // The value of Data in List #2 = 5[color=blue]
            > d=e;[/color]
            // d is now a reference to the List #2 object (just like 'e'). The
            // List #1 object is not referenced, and may now be garbage collected[color=blue]
            > d.Data=5;[/color]
            // The value of Data in List #2 = 5
            [color=blue]
            > The value of e.Data became 5,What does it do?if I don't want the
            > e.Data'value changed,what should i do?[/color]

            Don't change it? :)

            It's hard to know exactly what you're after here, so it's hard to
            suggest an alternative. But the other poster had a good point in
            suggesting walking through a good tutorial. Object references are at
            the heart of C#, and it's a good idea to have a very solid handle on
            them.


            --
            David
            dfoster at
            hotpop dot com

            Comment

            • Ray Hsieh (Djajadinata)

              #7
              Re: Problem about operator = in C#

              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1

              | after the programme below ran,
              | List d=new List();
              d --> ListA

              | List e=new List();
              e --> ListB

              | e.Data=4;
              ListB.Data becomes 4

              | d=e;
              d --> ListB
              e --> ListB
              That is, both refer to List B now, so..

              | d.Data=5;
              ListB.Data becomes 5

              | if I don't want the
              | e.Data'value changed,what should i do?

              Well first you have to ask yourself what you want to achieve with that
              statement (d = e) ?

              | btw:the operator = can't be override in c#.

              Yeah, and you shouldn't :)

              RD


              Archer wrote:

              | I defined my class as:
              | class List
              | {
              | public List Next;
              | public int Data;
              | public List()
              | {
              | Data=0;
              | }
              | }
              | ¡­¡­¡­¡­¡­¡­¡­¡ ­
              | after the programme below ran,
              | List d=new List();
              | List e=new List();
              | e.Data=4;
              | d=e;
              | d.Data=5;
              | The value of e.Data became 5,What does it do?if I don't want the
              | e.Data'value changed,what should i do? btw:the operator = can't be
              override
              | in c#.Thank you
              |
              |

              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.2.3 (MingW32)
              Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

              iD8DBQE/m1eLwEwccQ4rWPg RAjtRAJwMluP4Pc h7YTvtC9IZQxKvn p0jJQCfYLh9
              VsNiSA8w9EEWh0o L/Zdq3+w=
              =HzXr
              -----END PGP SIGNATURE-----

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Problem about operator = in C#

                <Peter Koen <koen-newsreply&snusn u.at>> wrote:[color=blue]
                > And if you really want your own impl: a class is always passed by
                > reference. so you will need a struct for your desired behaviour.[/color]

                No, nothing is passed by reference unless you say so. There's a
                difference between pass-by-reference semantics and references being
                passed by value.

                See http://www.pobox.com/~skeet/csharp/parameters.html

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

                • Jon Skeet [C# MVP]

                  #9
                  Re: Problem about operator = in C#

                  Archer <xydarcher@etan g.com> wrote:[color=blue]
                  > I defined my class as:
                  > class List
                  > {
                  > public List Next;
                  > public int Data;
                  > public List()
                  > {
                  > Data=0;
                  > }
                  > }
                  > ¡­¡­¡­¡­¡­¡­¡­¡ ­
                  > after the programme below ran,
                  > List d=new List();
                  > List e=new List();
                  > e.Data=4;
                  > d=e;
                  > d.Data=5;
                  > The value of e.Data became 5,What does it do?if I don't want the
                  > e.Data'value changed,what should i do? btw:the operator = can't be override
                  > in c#.Thank you[/color]

                  If you want d and e to be references to independent lists, you need to
                  clone the list rather than having two references to the same object.

                  As other posters have said, it's vital to understand how reference
                  types and value types work. There's some information on this in

                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  and
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


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

                  • Bill McNeal

                    #10
                    Re: Problem about operator = in C#

                    Jon, please, could you give me a better explanation of the sentence below?

                    "Instance variables for a value type are stored in the same context as the
                    variable that declares the value type.
                    The memory slot for the instance effectively contains the slots for each
                    field within the instance. That means (...) that a struct variable declared
                    within a method will always be on the stack, whereas a struct variable which
                    is an instance field of a class will be on the heap"


                    It got a little bit confused for me. Thanks for your time.
                    --



                    ---------------
                    Valmir Cinquini
                    Italian Citizenship: visit http://www.cinquini.com.br


                    "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                    news:MPG.1a058d 0c743c8d1398994 a@msnews.micros oft.com...
                    Archer <xydarcher@etan g.com> wrote:[color=blue]
                    > I defined my class as:
                    > class List
                    > {
                    > public List Next;
                    > public int Data;
                    > public List()
                    > {
                    > Data=0;
                    > }
                    > }
                    > ¡­¡­¡­¡­¡­¡­¡­¡ ­
                    > after the programme below ran,
                    > List d=new List();
                    > List e=new List();
                    > e.Data=4;
                    > d=e;
                    > d.Data=5;
                    > The value of e.Data became 5,What does it do?if I don't want the
                    > e.Data'value changed,what should i do? btw:the operator = can't be[/color]
                    override[color=blue]
                    > in c#.Thank you[/color]

                    If you want d and e to be references to independent lists, you need to
                    clone the list rather than having two references to the same object.

                    As other posters have said, it's vital to understand how reference
                    types and value types work. There's some information on this in

                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    and
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


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

                    • Madhanmohan S

                      #11
                      Re: Problem about operator = in C#

                      Hi,
                      'struct' is value type and 'class' is reference type.
                      Thanks And Regards.
                      Madhanmohan S
                      "Archer" <xydarcher@etan g.com> wrote in message
                      news:eJ0iQN2mDH A.424@TK2MSFTNG P10.phx.gbl...[color=blue]
                      > What about the object i created for e?
                      > is it be released auto?And what is the difference between stuct & class in
                      > C#?
                      > "Peter Koen" <koen-newsreply&snusn u.at> дÈëÓʼþ
                      > news:ezfZRF2mDH A.360@TK2MSFTNG P12.phx.gbl...[color=green]
                      > > "Archer" <xydarcher@etan g.com> wrote in
                      > > news:Ow5aVB2mDH A.2964@tk2msftn gp13.phx.gbl:
                      > >[color=darkred]
                      > > > I defined my class as:
                      > > > class List
                      > > > {
                      > > > public List Next;
                      > > > public int Data;
                      > > > public List()
                      > > > {
                      > > > Data=0;
                      > > > }
                      > > > }
                      > > > ¡­¡­¡­¡­¡­¡­¡­¡ ­
                      > > > after the programme below ran,
                      > > > List d=new List();
                      > > > List e=new List();
                      > > > e.Data=4;
                      > > > d=e;
                      > > > d.Data=5;
                      > > > The value of e.Data became 5,What does it do?if I don't want the
                      > > > e.Data'value changed,what should i do? btw:the operator = can't be
                      > > > override in c#.Thank you
                      > > >
                      > > >
                      > > >[/color]
                      > >
                      > > What about using System.Collecti ons.ArrayList instead of constructing[/color][/color]
                      your[color=blue][color=green]
                      > > own List?
                      > >
                      > > And if you really want your own impl: a class is always passed by
                      > > reference. so you will need a struct for your desired behaviour.
                      > >
                      > > --
                      > > best regards
                      > >
                      > > Peter Koen
                      > > -----------------------------------
                      > > MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
                      > > http://www.kema.at[/color]
                      >
                      >[/color]


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: Problem about operator = in C#

                        Bill McNeal <BillMcNeal@Bol .com.br> wrote:[color=blue]
                        > Jon, please, could you give me a better explanation of the sentence below?
                        >
                        > "Instance variables for a value type are stored in the same context as the
                        > variable that declares the value type.
                        > The memory slot for the instance effectively contains the slots for each
                        > field within the instance. That means (...) that a struct variable declared
                        > within a method will always be on the stack, whereas a struct variable which
                        > is an instance field of a class will be on the heap"
                        >
                        >
                        > It got a little bit confused for me. Thanks for your time.[/color]

                        It means that if you have:

                        public class Foo
                        {
                        int x;

                        void Something()
                        {
                        int y = 5;
                        }
                        }

                        then the value of y is on the stack when Something is called, whereas
                        the value of x for any instance is stored with the rest of the
                        information about that instance, on the heap.

                        Does that help?

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

                        • Bill McNeal

                          #13
                          Re: Problem about operator = in C#

                          Yes, it does.

                          By the way, reading the articles you suggested, I'd like to answer one more
                          wuestion: What's the big deal using output parameters, if they behave like
                          reference parameters? Both need to have the variables assigned before their
                          use, and both change the variable's value in the calling code if the called
                          function change them.

                          So why use them?

                          Thanks again for your time.

                          --



                          ---------------
                          Valmir Cinquini






                          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                          news:MPG.1a06e7 fd9abf882198994 b@msnews.micros oft.com...[color=blue]
                          > Bill McNeal <BillMcNeal@Bol .com.br> wrote:[color=green]
                          > > Jon, please, could you give me a better explanation of the sentence[/color][/color]
                          below?[color=blue][color=green]
                          > >
                          > > "Instance variables for a value type are stored in the same context as[/color][/color]
                          the[color=blue][color=green]
                          > > variable that declares the value type.
                          > > The memory slot for the instance effectively contains the slots for each
                          > > field within the instance. That means (...) that a struct variable[/color][/color]
                          declared[color=blue][color=green]
                          > > within a method will always be on the stack, whereas a struct variable[/color][/color]
                          which[color=blue][color=green]
                          > > is an instance field of a class will be on the heap"
                          > >
                          > >
                          > > It got a little bit confused for me. Thanks for your time.[/color]
                          >
                          > It means that if you have:
                          >
                          > public class Foo
                          > {
                          > int x;
                          >
                          > void Something()
                          > {
                          > int y = 5;
                          > }
                          > }
                          >
                          > then the value of y is on the stack when Something is called, whereas
                          > the value of x for any instance is stored with the rest of the
                          > information about that instance, on the heap.
                          >
                          > Does that help?
                          >
                          > --
                          > Jon Skeet - <skeet@pobox.co m>
                          > http://www.pobox.com/~skeet
                          > If replying to the group, please do not mail me too[/color]


                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: Problem about operator = in C#

                            Bill McNeal <BillMcNeal@Bol .com.br> wrote:[color=blue]
                            > By the way, reading the articles you suggested, I'd like to answer one more
                            > wuestion: What's the big deal using output parameters, if they behave like
                            > reference parameters? Both need to have the variables assigned before their
                            > use[/color]

                            No they don't - out parameters don't need to have the variables
                            assigned before their use (in the calling code) and they *do* need to
                            have values assigned before the method completes normally (ie without
                            an exception). The method can't read the value of the variable until it
                            assigns it, either.

                            My article (http://www.pobox.com/~skeet/csharp/parameters.html) gives a
                            little bit more detail on this, and an example - I suggest you play
                            around with the example code, changing out to ref, commenting out
                            assignments etc, and see what the differences are.
                            [color=blue]
                            > and both change the variable's value in the calling code if the called
                            > function change them.[/color]

                            Yes.

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

                            • Bill McNeal

                              #15
                              Re: Problem about operator = in C#

                              Ok Jon, thanks again for your reply. I have already played a little with the
                              source code provided by your article, and i understood all about outpu
                              parameters (it was easier than where ref and value parameters differs)

                              I just couldnt figure out which benefits I can get using output parameters
                              instead of reference parameters. Just when I dont have a value to assign to
                              a variable before pass it to a function? So what? I can assign some "dummy"
                              value and pass it to a function as a ref parameter.

                              Please, if there is something I dont know, them let me know.

                              Thanks again.

                              Valmir


                              Comment

                              Working...