none
this operator RRS feed

  • General discussion

  • I am studying for the 70-483 exam and was experimenting with the 'this' operator.  I am confused as to what causes only the first parameter to get changed.  Any explanation as to what the compiler is actually doing would be appreciated 

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading.Tasks;
    public class Example 
    {
        public string name;
        public void Equals(Example other, Example other2)
        {
            Console.WriteLine("param " + other.name);
            Console.WriteLine("param2 " + other2.name);
            Console.WriteLine("this  " + this.name);
            this.name = "this";
            Console.WriteLine("param " + other.name);
            Console.WriteLine("param2 " + other2.name);
            Console.WriteLine("this  " + this.name);
            Console.WriteLine();
        }
        public void Equals2(Example other, Example other2)
        {
            Console.WriteLine("param " + other.name);
            Console.WriteLine("param2 " + other2.name);
            Console.WriteLine("this  " + this.name);
            this.name = "this";
            Console.WriteLine("param " + other.name);
            Console.WriteLine("param2 " + other2.name);
            Console.WriteLine("this  " + this.name);
            Console.WriteLine();
        }
        public static void Main()
        {
            Example param = new Example();
            Example param2 = new Example();
            param.name = "param";
            param2.name = "param2";
            param.Equals(param, param2);
            param.Equals2(param, param2);
        }
    }
    

    Tuesday, October 30, 2018 11:30 PM

All replies