Esempio n. 1
0
 public static void GetNewGreenApple(ref Apple apple)
 {
     apple = new Apple(apple.weight, Color.Green);
 }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            #region reference types
            Console.WriteLine("======= Reference types ========");
            Apple redApple        = new Apple(10, Color.Red);
            Apple theSameRedApple = redApple;
            Console.WriteLine($"Weight of the red apple: {redApple.Weight}");
            Console.WriteLine($"Weight of the same red apple: {theSameRedApple.Weight}");
            redApple.Weight = 15;
            Console.WriteLine($"Weight of the red apple after change: {redApple.Weight}");
            Console.WriteLine($"Weight of the same red apple after change: {redApple.Weight}");

            #endregion
            Console.WriteLine();
            #region value types
            Console.WriteLine("======= Value types ========");
            int x = 10;
            int y = x;
            Console.WriteLine($"Value of x: {x}");
            Console.WriteLine($"Value of y: {y}");
            x++;
            Console.WriteLine($"Value of x after change: {x}");
            Console.WriteLine($"Value of y after change: {y}");
            #endregion
            Console.WriteLine();
            #region Use of TotalWeight static method
            Console.WriteLine("======== Usage of static TotalWeight method =========");
            Apple        greenApple = new Apple(20, Color.Green);
            List <Apple> apples     = new List <Apple>();
            apples.Add(redApple);
            apples.Add(greenApple);
            Console.WriteLine($"Weight of red apple: { redApple.Weight }");
            Console.WriteLine($"Weight of green apple: { greenApple.Weight }");
            Console.WriteLine($"Total weight of apples: {Apple.TotalWeight(apples)}");
            #endregion
            Console.WriteLine();
            #region Use of out parameter modifiers
            Console.WriteLine("======== Use of out modifier =========");
            Apple clonedGreenApple;
            Apple.CloneApple(greenApple, out clonedGreenApple);
            clonedGreenApple.Color = Color.Red;
            Console.WriteLine($"Change the color of cloned apple to red: { clonedGreenApple.Color }");
            Console.WriteLine($"Color of the original apple remains the same: { greenApple.Color }");
            #endregion
            Console.WriteLine();
            #region Use of ref parameter modifier
            Console.WriteLine("========= Use of ref modifier =========");
            Apple.GetNewGreenApple(ref redApple);
            Console.WriteLine($"The color of new green apple: { redApple.Color }");
            #endregion
            Console.WriteLine();
            #region Use of boxing and unboxing
            AppleWithPrice appleWithIntPrice   = new AppleWithPrice(20, Color.Green, 30);
            AppleWithPrice appleWithFloatPrice = new AppleWithPrice(10, Color.Red, 20.5f);
            int            intPrice            = (int)appleWithIntPrice.Price;
            float          floatPrice          = (float)appleWithFloatPrice.Price;
            #endregion
            #region Use of static constructor
            Console.WriteLine("====== Use of static constructor ========");
            StaticConstructorExample staticConstructorExample  = new StaticConstructorExample();
            StaticConstructorExample staticConstructorExample1 = new StaticConstructorExample();
            #endregion
            #region Threads
            Console.WriteLine("========= Use of threads ==========");
            Thread newThread = new Thread(new ThreadStart(RunMeInNewThread));
            newThread.Start();
            for (int i = 0; i < 5; i++)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("*");
                Console.WriteLine(" *");
                Console.WriteLine("  *");
                Console.WriteLine("   *");
                Console.WriteLine("    *");
                Thread.Sleep(400);
            }

            #endregion
        }
Esempio n. 3
0
 public static void CloneApple(Apple originalApple, out Apple clonedApple)
 {
     clonedApple = new Apple(originalApple.weight, originalApple.Color);
 }