parameters in functions

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

    #1

    parameters in functions

    Hi everyone,
    I have a newbie question,
    Say I have :
    -----------------------------
    class Class1 {
    private int field1;
    private string field2;
    }

    class Class2 {
    public bool func1( Class1 cls1){
    return (cls1 == null);
    }
    }
    ------------------------------

    as I understand when func1 gets control cls1 gets cloned? I mean
    finally i get 2 identical objects, one inside of function scope and
    one outside?
    Did I understand it correctly?
    Serge
  • Jon Skeet [C# MVP]

    #2
    Re: parameters in functions

    Serge Blokhin <sergey.blokhin @ca.com> wrote:[color=blue]
    > Say I have :
    > -----------------------------
    > class Class1 {
    > private int field1;
    > private string field2;
    > }
    >
    > class Class2 {
    > public bool func1( Class1 cls1){
    > return (cls1 == null);
    > }
    > }
    > ------------------------------
    >
    > as I understand when func1 gets control cls1 gets cloned? I mean
    > finally i get 2 identical objects, one inside of function scope and
    > one outside?
    > Did I understand it correctly?[/color]

    No. See http://www.pobox.com/~skeet/csharp/parameters.html for a fairly
    close look at this kind of thing. Basically it all comes down to
    understanding references and reference types.

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

    • Bjorn Abelli

      #3
      Re: parameters in functions


      "Serge Blokhin" wrote...
      [color=blue]
      > class Class2 {
      > public bool func1( Class1 cls1){
      > return (cls1 == null);
      > }
      > }[/color]
      [color=blue]
      > as I understand when func1 gets control
      > cls1 gets cloned? I mean finally i get 2
      > identical objects, one inside of function
      > scope and one outside?
      > Did I understand it correctly?[/color]

      No you didn't.

      The parameter is passed "by value", that is to say that the value of the
      parameter is copied into the local variable "cls1".

      However, the value of the parameter isn't the object itself, but a
      *reference* to it. Hence it is still only one object.

      // Bjorn A


      Comment

      • Serge Blokhin

        #4
        Re: parameters in functions

        Bjorn,
        If you are right, that means I could change outside object from my
        function simply call its methods, but it is not happening. It is only
        going to happen if I use ref parameter
        Thanks
        Serge

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Serge Blokhin

          #5
          Re: parameters in functions

          Jon,
          According to your link,
          I get another object in the function, right?
          so from memory consumption perspective I use twice more memory compare
          to if I declared function as:

          class Class2{
          public bool func1( ref Class1 cls1){
          return (cls1 == null);
          }
          }
          Right?
          Thanks
          Serge



          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Frank Oquendo

            #6
            Re: parameters in functions

            Serge Blokhin wrote:[color=blue]
            > Bjorn,
            > If you are right, that means I could change outside object from my
            > function simply call its methods, but it is not happening. It is only
            > going to happen if I use ref parameter[/color]

            That all depends on what you're doing with that parameter. Any
            assignments will have no effect in the calling method as you are
            assigning an object reference to a local variable. So this would have no
            noticeable effect:

            myParameter = new object();

            However, using the object's properties and methods will yield a lasting
            change if any of those properties or methods modify the object. So this
            would have a noticeable effect:

            myParameter.Tit le = "New Title";

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


            (Pull the pin to reply)


            Comment

            • Frank Oquendo

              #7
              Re: parameters in functions

              Serge Blokhin wrote:[color=blue]
              > Jon,
              > According to your link,
              > I get another object in the function, right?[/color]

              No. You get a copy of the object reference, not the object.

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


              (Pull the pin to reply)


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: parameters in functions

                Serge Blokhin <sergey.blokhin @ca.com> wrote:[color=blue]
                > According to your link,
                > I get another object in the function, right?[/color]

                No, you get a distinct variable, and both variables have the same value
                - but that value is a reference to a single object.
                [color=blue]
                > so from memory consumption perspective I use twice more memory compare
                > to if I declared function as:
                >
                > class Class2{
                > public bool func1( ref Class1 cls1){
                > return (cls1 == null);
                > }
                > }
                > Right?[/color]

                No. Read the page again, very carefully, paying particular attention to
                the early sections. Let me know if it doesn't become any clearer.

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

                • Bjorn Abelli

                  #9
                  Re: parameters in functions

                  > Serge Blokhin wrote:[color=blue][color=green]
                  > > Bjorn,
                  > > If you are right, that means I could
                  > > change outside object from my
                  > > function simply call its methods,[/color][/color]

                  That's correct.
                  [color=blue][color=green]
                  > > but it is not happening.[/color][/color]

                  Then you're probably trying to initialize a new object for the variable,
                  which results in a *replacement* of the reference you got. If you simply
                  "work" on the object, any changes still exists in the object when you leave
                  the method:

                  static void method1(Class1 cls1)
                  {
                  cls1.attribute = "method1";
                  }
                  [color=blue][color=green]
                  > > It is only going to happen
                  > > if I use ref parameter[/color][/color]

                  The default behaviour is that the parameter is passed by value, that is that
                  the value of the parameter is copied into the local variable, but for
                  objects from classes, that means that the value isn't the object, but the
                  reference to it.

                  When you use the ref keyword, you should conceptually think of it as if
                  you're working with the *same* variables as the one you passed as a
                  parameter, not a copy at all.

                  static void method2(ref Class1 cls1)
                  {
                  cls1 = new Class1(); // see cls1 as an "alias"
                  // for the outside *variable*.
                  cls1.attribute = "method2";
                  }

                  If you leave out the ref keyword in example method2, the argumentlist is
                  really redundant:

                  static void method3(Class1 cls1)
                  {
                  cls1 = new Class1();
                  cls1.attribute = "method3";
                  }

                  is functionally equal to:

                  static void method3()
                  {
                  Class1 cls1 = new Class1();
                  cls1.attr = "method3;
                  }

                  ....although the latter avoids the then unnecessary operation of copying the
                  variable value from the parameter. (Note that the last two examples are
                  unnecessary altogether, since the effect of them vanishes as they go out of
                  scope...)

                  // Bjorn A


                  Comment

                  Working...