Ejemplo n.º 1
0
        private static int y = 5; // have to define 'static', otherwise y couldn't be accessed by the static method 'Main'

        #endregion Fields

        #region Methods

        public static void Main(string[] args)
        {
            // Case 1:
            int x = 6; // can not declare public/private/static on this, why?
            int z = 900;
            changeValue (x); // x is a internal variable to the Main method.
            changeValueRef (ref z); // ref will force z be treated as reference type. The "ref" key word need exist in function and when called

            changeValue (y); // y is another static member on the Main class.

            Console.WriteLine ("primiate value make a copy, x: {0}", x); // x is still 6. because they are primative
            Console.WriteLine ("primiate value make a copy, y: {0}", y); // y is still 5
            Console.WriteLine ("primiate value make a copy, z: {0}", z); // z will be changed to 10

            // Case 2:
            changeGlobalValue ();
            Console.WriteLine ("use public static class to mimic global variables, x: {0}", Globals.x);

            // Case 3:
            Globals2 g2 = new Globals2 ();
            changeGlobalValue2 (g2);
            Console.WriteLine ("Pass non-static class as variable, but need instantiate, g2.x: {0}", g2.x);
        }
Ejemplo n.º 2
0
 // Case 3:
 // a non-static class can be passed in as parameter. but need to be instantiat when called
 private static int changeGlobalValue2(Globals2 g)
 {
     g.x = 56;
     return g.x;
 }