Пример #1
0
        private static void ClassAndStruct()
        {
            PointVal a; // PointVal a = new PointVal();

            a.X = 5;
            a.Y = 9;

            PointVal b = a;

            b.X = 4;
            b.Y = 8;

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

            PointRef c = new PointRef();

            c.X = 5;
            c.Y = 9;

            PointRef d = c;

            d.X = 4;
            d.Y = 8;

            c.LogValues();
            d.LogValues();

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

            var es1 = new EvilStruct();

            es1.Name  = "es1";
            es1.X     = 1;
            es1.Y     = 2;
            es1.PrRef = new PointRef()
            {
                X = 11, Y = 22
            };

            var es2 = es1;

            es2.Name = "es2";

            es1.LogValues();
            es2.LogValues();
            Console.WriteLine();

            es2.X       = 3;
            es2.Y       = 4;
            es2.PrRef.X = 33;
            es2.PrRef.Y = 44;

            es1.LogValues();
            es2.LogValues();
            Console.WriteLine();
        }
Пример #2
0
        private static void NullableStructures()
        {
            PointVal pv;
            // Console.WriteLine(pv.X); // Not initialized
            PointRef pr = null;
            // Console.WriteLine(pr.X); // Null Pointer Exception

            PointVal?pv2 = null;

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

            PointVal pv4 = pv2.GetValueOrDefault();
        }