Example #1
0
 static void DoSomething(TestRef t)
 {
     t           = new TestRef();
     t.Something = "Not just a changed t, but a completely different TestRef object";
     t.n         = 10000000;
     Console.WriteLine("in funciton DoSomething(TestRef t): " + t.ToString());
 }
Example #2
0
        static void Main(string[] args)
        {
            // how to get an instance's class name
            ChildrenClass cd        = new ChildrenClass();
            BaseClass     bc        = cd;
            string        className = bc.GetType().Name;



            //Example 1.
            //    In this example, the program receives a string from the user and displays it inside a message box.
            //    The program uses the MessageBox method imported from the User32.dll library.
            string myString;

            Console.Write("Enter your message: ");
            myString = Console.ReadLine();
            var x = MessageBox((IntPtr)0, myString, "My Message Box", 0);


            // passed by refference behaviour
            TestRef t = new TestRef();

            t.Something = "Foo";
            t.n         = 10;
            Console.WriteLine("--------------------- has ref keyword -------------------------");
            DoSomething(ref t);
            Console.WriteLine("after funtion DoSomething(ref TestRef t): " + t.ToString());
            //output
            //--------------------- has ref keyword -------------------------
            //In funtion DoSomething(ref TestRef t): Not just a changed t, but a completely different TestRef object,10000000
            //after funtion DoSomething(ref TestRef t): Not just a changed t, but a completely different TestRef object,10000000


            Console.WriteLine("");
            Console.WriteLine("");

            Console.WriteLine("--------------------- no ref keyword --------------------------");

            t.Something = "Foo";
            t.n         = 10;
            DoSomething(t);
            Console.WriteLine("after funtion DoSomething(TestRef t): " + t.ToString());
            // output
            //-------------------- - no ref keyword --------------------------
            //in funciton DoSomething(TestRef t): Not just a changed t, but a completely different TestRef object,10000000
            //after funtion DoSomething(TestRef t): Foo,10



            Console.ReadKey();
        }