Store a reference to an object

前端 未结 4 1353
庸人自扰
庸人自扰 2021-01-04 13:59

A bit of a weird question but I was wondering anyone could help...

In C++, I could do something like this

class MyOtherClass
{
     private:
                 


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 14:48

    adding comment for future me as sometime I also forget, also for someone new in c# want to visualize "c++ pointer" vs "c# object reference" vs "C# object reference By Ref". Notice how Passing By Ref(Last method call) and assigning new obj, changes original object.

    using System;
    
    public class Emp
    {
        public int TimeSpentInCompany {get; set;}
    }
    
    public class Program
    {
        public static void Main()
        {
            
            Emp t1 = new Emp{TimeSpentInCompany = 5};
            Console.WriteLine("original obj before method call-->" + t1.TimeSpentInCompany);
            
            // Test is one by one
            
            // var res = PassObject_SimpleUpdateMemberAndPrint(t1);
            // var res = PassObject_SimpleUpdateObjectAndPrint(t1);
            // var res = PassObjectByRef_SimpleUpdateMemberAndPrint(ref t1);
            var res = PassObjectByRef_SimpleUpdateObjectAndPrint(ref t1);
            
            Console.WriteLine("original obj after method call-->" + t1.TimeSpentInCompany);
            Console.WriteLine("obj from method response-->" + res.TimeSpentInCompany);
        }
        
        static Emp PassObject_SimpleUpdateMemberAndPrint(Emp data)
        {
            /*
                original obj before method call-->5
                in method before modification obj member--> 5
                in method AFTER modification obj member--> 9
                original obj after method call-->9
                obj from method response-->9
            */      
            Console.WriteLine("in method before modification obj member--> "+ data.TimeSpentInCompany);
            data.TimeSpentInCompany += 4;
            Console.WriteLine("in method AFTER modification obj member--> "+ data.TimeSpentInCompany);
            return data;
        }   
        
        static Emp PassObject_SimpleUpdateObjectAndPrint(Emp data)
        {
            /*
                original obj before method call-->5
                in method before assigning new obj --> 5
                in method AFTER assigning new obj --> 9
                original obj after method call-->5
                obj from method response-->9
            */
            Console.WriteLine("in method before assigning new obj --> "+ data.TimeSpentInCompany);
            data = new Emp{TimeSpentInCompany = 9};
            Console.WriteLine("in method AFTER assigning new obj --> "+ data.TimeSpentInCompany);
            return data;
        }
        
        static Emp PassObjectByRef_SimpleUpdateMemberAndPrint(ref Emp data)
        {
            /*
                original obj before method call-->5
                in method before modification obj member--> 5
                in method AFTER modification obj member--> 9
                original obj after method call-->9
                obj from method response-->9
            */      
            Console.WriteLine("in method before modification obj member--> "+ data.TimeSpentInCompany);
            data.TimeSpentInCompany += 4;
            Console.WriteLine("in method AFTER modification obj member--> "+ data.TimeSpentInCompany);
            return data;
        }   
        
        static Emp PassObjectByRef_SimpleUpdateObjectAndPrint(ref Emp data)
        {
            /*
                original obj before method call-->5
                in method before assigning new obj --> 5
                in method AFTER assigning new obj --> 9
                original obj after method call-->9
                obj from method response-->9
            */
            Console.WriteLine("in method before assigning new obj --> "+ data.TimeSpentInCompany);
            data = new Emp{TimeSpentInCompany = 9};
            Console.WriteLine("in method AFTER assigning new obj --> "+ data.TimeSpentInCompany);
            return data;
        }
    }
    

提交回复
热议问题