Object passed as parameter to another class, by value or reference?

前端 未结 7 1651
闹比i
闹比i 2021-02-18 21:12

In C#, I know that by default, any parameters passed into a function would be by copy, that\'s, within the function, there is a local copy of the parameter. But, what about when

相关标签:
7条回答
  • I found the other examples unclear, so I did my own test which confirmed that a class instance is passed by reference and as such actions done to the class will affect the source instance.

    In other words, my Increment method modifies its parameter myClass everytime its called.

    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            Console.WriteLine(myClass.Value);  // Displays 1
            Increment(myClass);
            Console.WriteLine(myClass.Value);  // Displays 2
            Increment(myClass);
            Console.WriteLine(myClass.Value);  // Displays 3           
            Increment(myClass);
            Console.WriteLine(myClass.Value);  // Displays 4
            Console.WriteLine("Hit Enter to exit.");
            Console.ReadLine();
        }
    
        public static void Increment(MyClass myClassRef)
        {
            myClassRef.Value++;
        }
    }
    
    public class MyClass
    {
        public int Value {get;set;}
        public MyClass()
        {
            Value = 1;
        }
    }
    
    0 讨论(0)
  • 2021-02-18 21:45

    In general, an "object" is an instance of a class, which is an "image"/"fingerprint" of a class created in memory (via New keyword).
    The variable of object type refers to this memory location, that is, it essentially contains the address in memory.
    So a parameter of object type passes a reference/"link" to an object, not a copy of the whole object.

    0 讨论(0)
  • 2021-02-18 21:46

    If you need to copy object please refer to object cloning, because objects are passed by reference, which is good for performance by the way, object creation is expensive.

    Here is article to refer to: Deep cloning objects

    0 讨论(0)
  • 2021-02-18 21:50

    "Objects" are NEVER passed in C# -- "objects" are not values in the language. The only types in the language are primitive types, struct types, etc. and reference types. No "object types".

    The types Object, MyClass, etc. are reference types. Their values are "references" -- pointers to objects. Objects can only be manipulated through references -- when you do new on them, you get a reference, the . operator operates on a reference; etc. There is no way to get a variable whose value "is" an object, because there are no object types.

    All types, including reference types, can be passed by value or by reference. A parameter is passed by reference if it has a keyword like ref or out. The SetObject method's obj parameter (which is of a reference type) does not have such a keyword, so it is passed by value -- the reference is passed by value.

    0 讨论(0)
  • 2021-02-18 22:02

    Objects will be passed by reference irrespective of within methods of same class or another class. Here is a modified version of same sample code to help you understand. The value will be changed to 'xyz.'

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        public class Employee
        {
            public string Name { get; set; }
        }
    
        public class MyClass
        {
            public Employee EmpObj;
    
            public void SetObject(Employee obj)
            {
                EmpObj = obj;
            }
        }
    
        public class Program
        {
           static void Main(string[] args)
            {
                Employee someTestObj = new Employee();
                someTestObj.Name = "ABC";
    
                MyClass cls = new MyClass();
                cls.SetObject(someTestObj);
    
                Console.WriteLine("Changing Emp Name To xyz");
                someTestObj.Name = "xyz";
    
                Console.WriteLine("Accessing Assigned Emp Name");
                Console.WriteLine(cls.EmpObj.Name); 
    
               Console.ReadLine();
           }       
        }
     }
    
    0 讨论(0)
  • 2021-02-18 22:07

    An Object if passed as a value type then changes made to the members of the object inside the method are impacted outside the method also. But if the object itself is set to another object or reinitialized then it will not be reflected outside the method. So i would say object as a whole is passed as Valuetype only but object members are still reference type.

            private void button1_Click(object sender, EventArgs e)
        {
            Class1 objc ;
             objc = new Class1();
            objc.empName = "name1";
            checkobj( objc);
            MessageBox.Show(objc.empName);  //propert value changed; but object itself did not change
        }
        private void checkobj ( Class1 objc)
        {
            objc.empName = "name 2";
            Class1 objD = new Class1();
            objD.empName ="name 3";
            objc = objD ;
            MessageBox.Show(objc.empName);  //name 3
        }
    
    0 讨论(0)
提交回复
热议问题