How come changes to properties of value types persisted across method calls only if..

This is a discussion on How come changes to properties of value types persisted across method calls only if.. within the C# Programming forums, part of the General Programming Boards category; I know that when it comes to references types (ie: classes), changes to properties of said reference types are persisted ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    How come changes to properties of value types persisted across method calls only if..

    I know that when it comes to references types (ie: classes), changes to properties of said reference types are persisted across method calls, regardless of the type of the property.

    I recently noticed that the same is not true for value types (ie: structs). That is, changes to properties of value types are persisted across method calls only if said properties are non-built in reference types.

    For example:

    Code:
    namespace N
    {
    class M
    {
    public int k = 0;
    }
    
    class C
    {
    public string s;
    public int i;
    public M m;
    
    public C(string s, int i, M m)
    {
    this.s = s;
    this.i = i;
    this.m = m;
    }
    }
    
    struct S
    {
    public string s;
    public int i;
    public M m;
    
    public S(string s, int i, M m)
    {
    this.s = s;
    this.i = i;
    this.m = m;
    }
    }
    
    class Program
    {
    
    static void Main()
    {
    C c = new C("A", 0, new M());
    S s = new S("A", 0, new M());
    
    Console.WriteLine(c.s + ", " + c.i + ", " + c.m.k); //A, 0, 0
    Console.WriteLine(s.s + ", " + s.i + ", " + s.m.k); //A, 0, 0
    
    Change(c);
    Change(s);
    
    Console.WriteLine(c.s + ", " + c.i + ", " + c.m.k); //AA, 1, 1
    Console.WriteLine(s.s + ", " + s.i + ", " + s.m.k); //A, 0, 1
    
    Console.ReadKey();
    }
    
    static void Change(C x)
    {
    x.s = "AA";
    x.i++;
    x.m.k++;
    
    }
    
    static void Change(S x)
    {
    x.s = "AA";
    x.i++;
    x.m.k++;
    }
    
    }
    }
    Not sure if the above code will compile (maybe I made typos somewhere ). But if it does compile you will find that changes to properties of value types persisted across method calls only in cases where those properties are non-built-in reference types. How come?
    Last edited by y99q; 10-11-2011 at 10:23 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    It should be similiar to C: the values are copied, hence changes to them only affect the copy in the function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class method calls
    By Shingetsu Kurai in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2011, 08:52 PM
  2. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 10:35 AM
  3. Delegate Calling a method that Calls a delegate.
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 05-05-2008, 12:59 AM
  4. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  5. No properties in c++?
    By MisterT in forum C++ Programming
    Replies: 19
    Last Post: 08-06-2006, 08:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21