编辑代码

using System;

namespace ClassLearning{
    public class Stock
    {
        private decimal currentPrice;         

        public decimal CurrentPrice   
        {
            get { return currentPrice; }
            set { currentPrice = value; }
        }

        public decimal calculatePrice(decimal increasement) {
            currentPrice += increasement;
            return currentPrice;
        }

        public decimal calculatePrice(int decreasement) {
            currentPrice -= decreasement;
            return currentPrice;
        }

    }

    
    class Rectangle
    {
        public readonly float Width, Height;
        public Rectangle (float width, float height)
        {
            Width = width;
            Height = height;
        }
        public void Deconstruct (out float width, out float height)
        {
            width = Width;
            height = Height;
        }
    }

    public class Bunny
    {
        public string Name;
        public bool LikesCarrots;
        public bool LikesHumans;

        public Bunny () {}
        public Bunny (string n) { Name = n; }
    }

    public class Test
    {
        string name;
        public Test (string name) { this.name = name; }
    }

    class Foo
    {
        public static Foo Instance = new Foo();
        public static int X = 3;

        static Foo() { Console.WriteLine ($"I'm in static constructor with {X}"); }   
        Foo() { Console.WriteLine ($"I'm in constructor with {X}"); }    // 0

        ~Foo() => Console.WriteLine ("Foo Finalizing");
    }

    public class Asset
    {
        public string Name;
        public virtual decimal Liability => 0;
    }

    public class Stock1 : Asset  
    {
        public long SharesOwned;
    }

    public class House : Asset  
    {
        public decimal Mortgage;
        public override decimal Liability => Mortgage;
    }

    public abstract class AbAsset
    {
        public abstract decimal NetValue { get; }
    }

    public class RealStock : AbAsset
    {
        public long SharesOwned;
        public decimal CurrentPrice;
        public override decimal NetValue => CurrentPrice * SharesOwned;
    }
    public class BaseClass
    {
        public int X;
        public BaseClass () { }
        public BaseClass (int x) { 
            Console.WriteLine($"BaseClass {X}");
            this.X = x; 
            
        }
    }
    public class SubClass : BaseClass { }

    public class SubClass1 : BaseClass
    {
        public SubClass1 (int x) : base (x) { 
            Console.WriteLine($"SubClass1 {X}");
        }
    }

    public class Stack
    {
        int position;
        object[] data = new object[10];
        public void Push (object obj)   { data[position++] = obj;  }
        public object Pop()               { return data[--position]; }
    }
    

    public class HelloClass
    {

        public static void WriteCubes()
        {
            Console.WriteLine (Cube (3));
            Console.WriteLine (Cube (4));
            Console.WriteLine (Cube (5));

            int Cube (int value) => value * value * value;
        }

        public static void display (Asset asset)
        {
          System.Console.WriteLine (asset.Name);
        }

        public static void Main()
        {
            Stock stockA = new Stock();
            stockA.CurrentPrice = 2.0M;
            Console.WriteLine($"The price of stockA is {stockA.CurrentPrice}");
            WriteCubes();
            var rect = new Rectangle (3, 4);
            (float width, float height) = rect;          
            Console.WriteLine (width + " " + height);    
            Bunny b1 = new Bunny { Name="Bo", LikesCarrots=true, LikesHumans=false };
            Console.WriteLine(b1.Name);
            Bunny b2 = new Bunny ("Bob")      { LikesCarrots=true, LikesHumans=false };
            Console.WriteLine(b2.Name);
            Foo test = Foo.Instance;
            Console.WriteLine($"GetType of Foo is {test.GetType()}.");
            Console.WriteLine($"nameof of test is {nameof(test)}.");
            Stock1 msft = new Stock1 { Name="MSFT",SharesOwned=1000 };
            Console.WriteLine (msft.Name);           
            Console.WriteLine (msft.SharesOwned);   
            House mansion = new House { Name="Mansion",Mortgage=250000 };
            Console.WriteLine (mansion.Name);       
            Console.WriteLine (mansion.Mortgage);   
            display(msft);
            display(mansion);
            Stock1 msft1 = new Stock1();
            Asset asset = msft1;                      
            Stock1 stock = (Stock1)asset;                
            Console.WriteLine (stock.SharesOwned);      
            Console.WriteLine (stock == asset);           
            Console.WriteLine (stock == msft1);        
            Asset a1 = new Asset();
            Stock1 s1 = a1 as Stock1;       
            try{
                Stock1 s2 = (Stock1)a1;
            }
            catch(Exception ex) {
            }

            if (asset is Stock1 s3){
                Console.WriteLine (s3.SharesOwned);
            }

            House mansion1 = new House { Name="McMansion", Mortgage=250000 };
            Asset a2 = mansion1;
            Console.WriteLine (mansion1.Liability); 
            Console.WriteLine (a2.Liability);        
            SubClass1 sub = new SubClass1 (123);
            Stack stack = new Stack();
            stack.Push ("sausage");
            string s = (string) stack.Pop();  
            stack.Push (3);
            int three = (int) stack.Pop();
            Console.WriteLine (s);              
            Console.WriteLine (three);            
                
        }
    }
}