Thursday, March 3, 2016

Example of ref on reference type

    class Program
    {
        static public void change(List<string> thing)
        {
            thing[0] += "  adjusted";
            List<string> temp = new List<string>();
            temp.Add("change");
            thing = temp;
        }

        static public void refChange(ref List<string> thing)
        {
            List<string> temp = new List<string>();
            temp.Add("refChange");
            thing = temp;
        }
       
        static void Main(string[] args)
        {
            List<string> temp = new List<string>();
            temp.Add("hody doe");
            change(temp);
            Console.WriteLine(temp[0]);
            refChange(ref temp);
            Console.WriteLine(temp[0]);


        }
    }

No comments:

Post a Comment