Example #1
0
        public static void ValRefTypesDemo()
        {
            EvilStruct es1 = new EvilStruct();
            //es1.PointRef = new PointRef() { X = 1, Y = 2 };
            //es1.PointRef.X = 1;
            //es1.PointRef.Y = 2;

            EvilStruct es2 = es1;

            Console.WriteLine($"es1.PointRef.X ={es1.PointRef.X}, es1.PointRef.Y ={es1.PointRef.Y}");
            Console.WriteLine($"es2.PointRef.X ={es2.PointRef.X}, es2.PointRef.Y ={es2.PointRef.Y}");

            es2.PointRef.X = 42;
            es2.PointRef.Y = 45;
            Console.WriteLine($"es1.PointRef.X ={es1.PointRef.X}, es1.PointRef.Y ={es1.PointRef.Y}");
            Console.WriteLine($"es2.PointRef.X ={es2.PointRef.X}, es2.PointRef.Y ={es2.PointRef.Y}");

            PointVal a; // same as PointVal a = new PointVal();

            a.X = 3;
            a.Y = 5;

            PointVal b = a;

            b.X = 7;
            b.Y = 10;

            a.LogValues();
            b.LogValues();

            Console.WriteLine("After structers");

            PointRef c = new PointRef();

            c.X = 3;
            c.Y = 5;

            PointRef d = c;

            d.X = 7;
            d.Y = 10;

            c.LogValues();
            d.LogValues();
        }
Example #2
0
        static void NRE_NullableValTypeDemo()
        {
            PointVal?pv = null;

            if (pv.HasValue)
            {
                PointVal pv2 = pv.Value;
                Console.WriteLine(pv.Value.X);
                Console.WriteLine(pv2.X);
            }
            else
            {
            }
            PointVal pv3 = pv.GetValueOrDefault();

            PointRef c = null;;

            Console.WriteLine(c.X); //NullReferenceException
        }