Пример #1
0
        static void Main(string[] args)
        // When creating a new variable remember to put new as part of the code to allocate memory to the variable.
        {   /*
             * // Part 1 - Classes
             * var john = new Person();
             * john.FirsName = "John";
             * john.LastName = "Smith";
             * john.Introduce();
             *
             * Calculator calculator = new Calculator();
             * var result = calculator.Add(1, 2);
             * Console.WriteLine(result);
             */
            /*
             * // Part 2 - Arrays
             * var numbers = new int[3];
             * numbers[0] = 1;
             * Console.WriteLine(numbers[0]);
             * Console.WriteLine(numbers[1]); // Will be 0 because it is the first value in an int. The default value if not other is set.
             * Console.WriteLine(numbers[2]); // Will be 0 because it is the first value in an int. The default value if not other is set.
             *
             * var names = new string[3] { "Jack", "John", "Mary" };
             * Console.WriteLine(names[2]);// remember C# is 0 indexed. So values are 0,1,2. Value for number 2 will be Mary
             */
            /*
             * // Part 3 - Strings
             * int number = 1; // Int is part of the struct type - primitive type
             * Int32 integer = 2;
             *
             * string firstName = "Claus"; // string is a class in the system namespace
             * String lastName = "Petraeus";
             *
             * var fullName = string.Format("My name is {0} {1}", firstName, lastName);
             * Console.WriteLine(fullName);
             *
             * var firstNames = new string[3] { "Jack", "John", "Mary" };
             * var formattedNames = string.Join(",", firstNames);
             * Console.WriteLine(formattedNames);
             *
             * // Verbatim strings: Use it so you don't ned to use escape characters
             * var text = @"Hi John
             * Look into the following paths
             * c:\folder1\folder2
             * c:\folder3\folder4";
             * Console.WriteLine(text);
             */
            // Part 4 - Enums, reference types and value types
            var method = ShippingMethod.Express;

            Console.WriteLine((int)method);
            Console.WriteLine(method);

            var methodId = 3;

            Console.WriteLine((ShippingMethod)methodId);

            // Parse a string to an Enum
            Console.WriteLine(method.ToString());
            var methodName     = "Express";
            var shippingMethod = (ShippingMethod)Enum.Parse(typeof(ShippingMethod), methodName);

            Console.WriteLine(shippingMethod);

            // Value types are copied in memory. So each new variable will have a unique place in memory
            var a = 10;
            var b = a;

            b++;
            Console.WriteLine("a:{0}, b:{1}", a, b);

            //Reference types are refrenced in memory. So each new variable will reference the same variable in memory
            var array1 = new int[3] {
                1, 2, 3
            };
            var array2 = array1;

            array2[0] = 0;
            Console.WriteLine("array1:{0}, array2:{1}", array1[0], array2[0]); // Both array1 and array2 were changed because the reference the same variable in memory. The are dependent on one another.

            int number = 1;

            Increment(number);
            Console.WriteLine(number); // The value here will be 1 because the Increment method is a value type so the value is not incremented by 10

            var person = new AgeIncrease()
            {
                Age = 20
            };

            MakeOld(person);
            Console.WriteLine(person.Age); // This value will be 30 because we used a reference type
        }
Пример #2
0
 public static void MakeOld(AgeIncrease person)
 {
     person.Age += 10;
 }