示例#1
0
        static void printStructAndClass()
        {
            var userStruct = new UserStruct("Ann", 18);

            Console.WriteLine("Структура");
            userStruct.DisplayInfo(); // Name: Ann  Age: 18
            changeStruct(userStruct);
            userStruct.DisplayInfo(); // Name: Ann  Age: 18
            changeStruct(ref userStruct);
            userStruct.DisplayInfo(); //Name: Jane  Age: 34

            var userStruct2 = new UserStruct {
                name = "Sam", age = 31
            };

            userStruct2.DisplayInfo();
            var userStruct3 = new UserStruct();

            userStruct3.DisplayInfo(); //Name:   Age: 0
            userStruct3.name = "Tom";
            userStruct3.age  = 68;
            userStruct3.DisplayInfo(); //Name: Tom  Age: 68

            var userClass = new UserClass("Ann", 18);

            Console.WriteLine("Класс");
            userClass.DisplayInfo(); // Name: Ann  Age: 18
            changeClass(userClass);
            userClass.DisplayInfo(); // Name: Tarja  Age: 41
            changeClassRef(ref userClass);
            userClass.DisplayInfo(); // Name: Andrew  Age: 42
        }
示例#2
0
        static void changeStruct(ref UserStruct user)
        {
            user.name = "Jane";
            user.age  = 34;

            Console.WriteLine("\tPrint from method");
            Console.Write("\t");
            user.DisplayInfo();
            Console.WriteLine("\tEnd print from method");
        }
示例#3
0
        static void printCopiedStructAndClass()
        {
            Console.WriteLine("Структура");
            var userStruct1 = new UserStruct();
            var userStruct2 = new UserStruct("Kate", 14);

            userStruct1     = userStruct2;
            userStruct2.age = 16;
            userStruct1.DisplayInfo();
            userStruct2.DisplayInfo();

            Console.WriteLine("Класс");
            var userClass1 = new UserClass();
            var userClass2 = new UserClass("Kate", 14);

            userClass1     = userClass2;
            userClass2.age = 16;
            userClass1.DisplayInfo();
            userClass2.DisplayInfo();
        }