Beispiel #1
0
        private static void VariableTypes()
        {
            /*  C# supports two kinds of variable types:
                1. Value types - built-in primitive types and user-defined structs
                2. Reference types - Classes and other complex data types (variables of such types contain a reference to an instance)
            */

            int i = 10;
            int j = 20;
            // Since int is a value type, there is no connection between variables if one is assigned to another (assign-by-value) */
            int k = i;
            // A change made to i will not affect k
            i = 30;
            Console.WriteLine(i.ToString());
            Console.WriteLine(k.ToString());

            // Reference types on the other hand, point to memory addresses
            Person person1 = new Person("Evan");
            Person person2 = person1;
            // person2 now points to the same memory address as person1, so a change made to either affects the other
            person2.Name = "Riley";
            Console.WriteLine(person1.ToString());

            /*  The process of converting a value type to a reference type is called "boxing".
                The process of converting a reference type to a value type is called "unboxing".
                In Java, this is akin to creating a wrapper class for a primitive data type and converting.
            */
        }