Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Method parameters

Method parameters

Scheduled Pinned Locked Moved C#
csharpquestionlearning
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 1 Offline
    1 Offline
    1nsp1r3d
    wrote on last edited by
    #1

    Hi all, As I was reading C# Language Pocket Reference, it says that all parameters are passed by value(ASAIK the book targets .NET 1.0). I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case? Thanks, gamehack

    L C 3 Replies Last reply
    0
    • 1 1nsp1r3d

      Hi all, As I was reading C# Language Pocket Reference, it says that all parameters are passed by value(ASAIK the book targets .NET 1.0). I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case? Thanks, gamehack

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

      1nsp1r3d wrote:

      As I was reading C# Language Pocket Reference, it says that all parameters are passed by value(ASAIK the book targets .NET 1.0).

      Correct, unless the ref modifier is used.

      1nsp1r3d wrote:

      I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case?

      Nope. :) When a reference variable is passed to a method, it is by default passed by value. So if you were to do something like this:

      public void SomeMethod(string name)
      {
      name = "B";

      // Local name now points to "B"
      

      }

      Only the local name variable is changed. The reference variable passed to the method is not changed:

      //...

      string name = "A";

      SomeMethod(name);

      // name still references "A"

      So when a reference variable is passed to a method, it is passed by value by default. A copy of the variable is what is actually passed to the method. This local copy references the same object in memory as the original variable, but they are two distinct variables. Changes on the local variable itself does not change the original. However, if you use the ref modifier, you have a reference to a reference, so to speak. So given our above example:

      public void SomeMethod(ref string name)
      {
      name = "B";

      // name points to "B"
      

      }

      //...

      string name = "A";

      SomeMethod(ref name);

      // name now points to "B"

      Changes to the variable itself are reflected in the original.

      C 1 Reply Last reply
      0
      • L Leslie Sanford

        1nsp1r3d wrote:

        As I was reading C# Language Pocket Reference, it says that all parameters are passed by value(ASAIK the book targets .NET 1.0).

        Correct, unless the ref modifier is used.

        1nsp1r3d wrote:

        I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case?

        Nope. :) When a reference variable is passed to a method, it is by default passed by value. So if you were to do something like this:

        public void SomeMethod(string name)
        {
        name = "B";

        // Local name now points to "B"
        

        }

        Only the local name variable is changed. The reference variable passed to the method is not changed:

        //...

        string name = "A";

        SomeMethod(name);

        // name still references "A"

        So when a reference variable is passed to a method, it is passed by value by default. A copy of the variable is what is actually passed to the method. This local copy references the same object in memory as the original variable, but they are two distinct variables. Changes on the local variable itself does not change the original. However, if you use the ref modifier, you have a reference to a reference, so to speak. So given our above example:

        public void SomeMethod(ref string name)
        {
        name = "B";

        // name points to "B"
        

        }

        //...

        string name = "A";

        SomeMethod(ref name);

        // name now points to "B"

        Changes to the variable itself are reflected in the original.

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        string is a bad example, because it is immutable, so when you cannot actually change it. Each time you are creating a new string object.


        My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

        1 Reply Last reply
        0
        • 1 1nsp1r3d

          Hi all, As I was reading C# Language Pocket Reference, it says that all parameters are passed by value(ASAIK the book targets .NET 1.0). I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case? Thanks, gamehack

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          1nsp1r3d wrote:

          I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case?

          Yes, you are correct. Consider this small application:

          using System;

          namespace ConsoleApplication12
          {
          class A
          {
          private string someValue;

              public string SomeValue
              {
                  get
                  {
                      return this.someValue;
                  }
                  set 
                  {
                      this.someValue = value;
                  }
              }
          }
          
          class Class1
          {
              \[STAThread\]
              static void Main(string\[\] args)
              {
                  Console.WriteLine("In the Main method");
                  A mainA = new A();
                  mainA.SomeValue = "This was set in the main method";
                  Console.WriteLine(mainA.SomeValue);
                  AnotherMethod(mainA);
                  Console.WriteLine(mainA.SomeValue);
                  Console.ReadLine();
              }
          
              static void AnotherMethod(A parameterA)
              {
                  Console.WriteLine("In AnotherMethod");
                  Console.WriteLine(parameterA.SomeValue);
                  parameterA.SomeValue = "This value was set in AnotherMethod";
                  Console.WriteLine(parameterA.SomeValue);
                  Console.WriteLine("Exiting AnotherMethod");
              }
          }
          

          }

          The output would be:

          In the Main method
          This was set in the main method
          In AnotherMethod
          This was set in the main method
          This value was set in AnotherMethod
          Exiting AnotherMethod
          This value was set in AnotherMethod

          As you can see, the object of class A is passed by reference because when its property is updated in the method, the update is reflected in the Main method after the call. Does this help?


          My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

          L 1 Reply Last reply
          0
          • C Colin Angus Mackay

            1nsp1r3d wrote:

            I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case?

            Yes, you are correct. Consider this small application:

            using System;

            namespace ConsoleApplication12
            {
            class A
            {
            private string someValue;

                public string SomeValue
                {
                    get
                    {
                        return this.someValue;
                    }
                    set 
                    {
                        this.someValue = value;
                    }
                }
            }
            
            class Class1
            {
                \[STAThread\]
                static void Main(string\[\] args)
                {
                    Console.WriteLine("In the Main method");
                    A mainA = new A();
                    mainA.SomeValue = "This was set in the main method";
                    Console.WriteLine(mainA.SomeValue);
                    AnotherMethod(mainA);
                    Console.WriteLine(mainA.SomeValue);
                    Console.ReadLine();
                }
            
                static void AnotherMethod(A parameterA)
                {
                    Console.WriteLine("In AnotherMethod");
                    Console.WriteLine(parameterA.SomeValue);
                    parameterA.SomeValue = "This value was set in AnotherMethod";
                    Console.WriteLine(parameterA.SomeValue);
                    Console.WriteLine("Exiting AnotherMethod");
                }
            }
            

            }

            The output would be:

            In the Main method
            This was set in the main method
            In AnotherMethod
            This was set in the main method
            This value was set in AnotherMethod
            Exiting AnotherMethod
            This value was set in AnotherMethod

            As you can see, the object of class A is passed by reference because when its property is updated in the method, the update is reflected in the Main method after the call. Does this help?


            My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

            L Offline
            L Offline
            Leslie Sanford
            wrote on last edited by
            #5

            Colin Angus Mackay wrote:

            As you can see, the object of class A is passed by reference because when its property is updated in the method, the update is reflected in the Main method after the call.

            I'm sorry, but this isn't corrent. The variable mainA is passed by value to AnotherMethod. The parameterA is another variable that references the same object in memory as mainA. Changing the property value in AnotherMethod changes the object that is being referenced by both mainA and parameterA. That is why the change is reflected outside of AnotherMethod. Operations on parameterA itself are not reflected outside of that method:

            static void AnotherMethod(A parameterA)
            {
            Console.WriteLine("In AnotherMethod");
            Console.WriteLine(parameterA.SomeValue);
            parameterA = new A(); // Allocated new object.
            parameterA.SomeValue = "This value was set in AnotherMethod";
            Console.WriteLine(parameterA.SomeValue);
            Console.WriteLine("Exiting AnotherMethod");
            }

            If you were to pass mainA by reference, changes to parameterA itself would also change mainA.

            static void AnotherMethod(ref A parameterA)
            {
            Console.WriteLine("In AnotherMethod");
            Console.WriteLine(parameterA.SomeValue);
            parameterA = new A(); // Allocated new object.
            parameterA.SomeValue = "This value was set in AnotherMethod";
            Console.WriteLine(parameterA.SomeValue);
            Console.WriteLine("Exiting AnotherMethod");
            }

            C 1 Reply Last reply
            0
            • L Leslie Sanford

              Colin Angus Mackay wrote:

              As you can see, the object of class A is passed by reference because when its property is updated in the method, the update is reflected in the Main method after the call.

              I'm sorry, but this isn't corrent. The variable mainA is passed by value to AnotherMethod. The parameterA is another variable that references the same object in memory as mainA. Changing the property value in AnotherMethod changes the object that is being referenced by both mainA and parameterA. That is why the change is reflected outside of AnotherMethod. Operations on parameterA itself are not reflected outside of that method:

              static void AnotherMethod(A parameterA)
              {
              Console.WriteLine("In AnotherMethod");
              Console.WriteLine(parameterA.SomeValue);
              parameterA = new A(); // Allocated new object.
              parameterA.SomeValue = "This value was set in AnotherMethod";
              Console.WriteLine(parameterA.SomeValue);
              Console.WriteLine("Exiting AnotherMethod");
              }

              If you were to pass mainA by reference, changes to parameterA itself would also change mainA.

              static void AnotherMethod(ref A parameterA)
              {
              Console.WriteLine("In AnotherMethod");
              Console.WriteLine(parameterA.SomeValue);
              parameterA = new A(); // Allocated new object.
              parameterA.SomeValue = "This value was set in AnotherMethod";
              Console.WriteLine(parameterA.SomeValue);
              Console.WriteLine("Exiting AnotherMethod");
              }

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              Ah... Yes, I see what you mean. You are correct. I guess I made part of the mistake I was trying to show not to make. :doh: What I was trying to say was that you create an object in one method and pass it to another method. The other method can update the object and the changes would be reflected in the calling method. Of course, I failed to mention that if you assign a new instance of class A to the parameter value then it wouldn't be reflected because that would be local to the method. Anyway, everything's sorted out now.... I hope.... :-O


              My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

              L 1 Reply Last reply
              0
              • 1 1nsp1r3d

                Hi all, As I was reading C# Language Pocket Reference, it says that all parameters are passed by value(ASAIK the book targets .NET 1.0). I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)? Is that the case? Thanks, gamehack

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Okay - I guess I made some mistakes with my explanation.

                1nsp1r3d wrote:

                I thought that all reference types were passed by reference and all value types by value(enums and ints/floats/etc)?

                A reference type is passed by reference insofaras you can update the object in the method and the changes will be reflected back in the original, but you cannot assign a new object. If you use ref you are passing a reference to a reference and you can assign a new object in the method and have that new object reflected in the calling method.


                My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious -- modified at 11:03 Thursday 24th November, 2005

                1 Reply Last reply
                0
                • C Colin Angus Mackay

                  Ah... Yes, I see what you mean. You are correct. I guess I made part of the mistake I was trying to show not to make. :doh: What I was trying to say was that you create an object in one method and pass it to another method. The other method can update the object and the changes would be reflected in the calling method. Of course, I failed to mention that if you assign a new instance of class A to the parameter value then it wouldn't be reflected because that would be local to the method. Anyway, everything's sorted out now.... I hope.... :-O


                  My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                  L Offline
                  L Offline
                  Leslie Sanford
                  wrote on last edited by
                  #8

                  Colin Angus Mackay wrote:

                  Anyway, everything's sorted out now.... I hope....

                  It's kinda tricky to wrap your head around. I started out with C years ago where things were on a much lower level than in C#. So you would deal with pointers directly instead of the higher level references C# gives you. I can remember years ago doing something like this (in C):

                  void SomeMethod(int *pointer)
                  {
                  /* Allocate memory */
                  pointer = malloc(42);
                  }

                  And calling this function elsewhere:

                  int *p;

                  SomeMethod(p);

                  /* Oops!!! The variable p doesn't point to anything! */
                  p[0] = 0;

                  And wondering why memory wasn't being assigned to the variable passed into SomeMethod. I posted to comp.lang.c, not for the faint of heart, and was told rather sternly that the reason the variable being passed to the function wasn't being updated is that it was being passed by value. Think of the pointer has having a value, and that value is the address of the memory it points to. This value is what is passed to the function and is stored in the parameter variable. If I change the parameter variable itself, such as assigning memory to it, it's not reflected outside of the function. That's when I learned about the wonderful "pointers to pointers." So what I needed to do (and my C is really rusty, so pardon me if this isn't exactly correct), is:

                  void SomeMethod(int **pointer)
                  {
                  /* Allocate memory */
                  *pointer = malloc(42);
                  }

                  And calling this function elsewhere:

                  int *p;

                  SomeMethod(&p);

                  /* Yay! */
                  p[0] = 0;

                  Now, I'm not sure how things are under the covers in C#, so only take my C example as an analogy of how things might work in C#. But the concepts are the same. The reference variables reference, or point, to an object somewhere. When a reference variable is passed to a method, its value is passed to the method and stored in the parameter variable. So the parameter variable references the same object; it has the same value. Operations, such as setting properties, calling methods, etc., performed on on the parameter variable local to the method are performed on the same object referenced by the variable passed to the method. Pointers to pointers are the equivalent of using the ref modifier in C#. ref gives you a reference to a reference so that operations performed on the parameter variable are reflected outside of the method.

                  C 1 Reply Last reply
                  0
                  • L Leslie Sanford

                    Colin Angus Mackay wrote:

                    Anyway, everything's sorted out now.... I hope....

                    It's kinda tricky to wrap your head around. I started out with C years ago where things were on a much lower level than in C#. So you would deal with pointers directly instead of the higher level references C# gives you. I can remember years ago doing something like this (in C):

                    void SomeMethod(int *pointer)
                    {
                    /* Allocate memory */
                    pointer = malloc(42);
                    }

                    And calling this function elsewhere:

                    int *p;

                    SomeMethod(p);

                    /* Oops!!! The variable p doesn't point to anything! */
                    p[0] = 0;

                    And wondering why memory wasn't being assigned to the variable passed into SomeMethod. I posted to comp.lang.c, not for the faint of heart, and was told rather sternly that the reason the variable being passed to the function wasn't being updated is that it was being passed by value. Think of the pointer has having a value, and that value is the address of the memory it points to. This value is what is passed to the function and is stored in the parameter variable. If I change the parameter variable itself, such as assigning memory to it, it's not reflected outside of the function. That's when I learned about the wonderful "pointers to pointers." So what I needed to do (and my C is really rusty, so pardon me if this isn't exactly correct), is:

                    void SomeMethod(int **pointer)
                    {
                    /* Allocate memory */
                    *pointer = malloc(42);
                    }

                    And calling this function elsewhere:

                    int *p;

                    SomeMethod(&p);

                    /* Yay! */
                    p[0] = 0;

                    Now, I'm not sure how things are under the covers in C#, so only take my C example as an analogy of how things might work in C#. But the concepts are the same. The reference variables reference, or point, to an object somewhere. When a reference variable is passed to a method, its value is passed to the method and stored in the parameter variable. So the parameter variable references the same object; it has the same value. Operations, such as setting properties, calling methods, etc., performed on on the parameter variable local to the method are performed on the same object referenced by the variable passed to the method. Pointers to pointers are the equivalent of using the ref modifier in C#. ref gives you a reference to a reference so that operations performed on the parameter variable are reflected outside of the method.

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #9

                    Leslie Sanford wrote:

                    and my C is really rusty, so pardon me if this isn't exactly correct

                    I haven't done C++ in about 5 years now. I understand what you mean though.

                    Leslie Sanford wrote:

                    Well, hopefully some of that makes sense.

                    Absolutely. :-D


                    My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups