C# 7

2021-01-15 15:12

阅读:402

标签:star   cube   stat   orm   write   goto   rds   loop   color   

 static void LocalMethod()
        {
            Cube(100);
            void Cube(int x) => Console.WriteLine($"The cube of {x} is {x * x * x}");
        }

        
        static void GoToDemo()
        {
            int i = 1;
            startLoop:
            if(i5)
            {
                Console.WriteLine(i);
                i++;
                goto startLoop;
            }
        }

deconstructor

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;
        }
    }

 static void Main(string[] args)
        {
            var rect = new Rectangle(3, 4);
            (float width, float height) = rect;
            Console.WriteLine($"width:{width},height:{height}");
            Console.ReadLine();
        }
 static void Main(string[] args)
        {
            var rect = new Rectangle(3, 4);
            var (width, height) = rect;
            Console.WriteLine($"width:{width},height:{height}");
            Console.ReadLine();
        }
static void Main(string[] args)
        {
            var rect = new Rectangle(3, 4);
            (var width,var height) = rect;
            Console.WriteLine($"width:{width},height:{height}");
            Console.ReadLine();
        }
class Product
    {
        decimal currentPrice, sharesOwned;
        public decimal Worth
        {
            get
            {
                return currentPrice * sharesOwned;
            }
        }

        public decimal Worth2 => currentPrice * sharesOwned;

        public decimal Worth3
        {
            get => currentPrice * sharesOwned;
            set => sharesOwned = value / currentPrice;
        }

public decimal CurrentPrice { get; set; } = 123;


public int Maximum { get; } = 999;


    }


private decimal x;
        public decimal X
        {
            get
            {
                return x;
            }

            private set
            {
                x = Math.Round(value, 2);
            }
        }

Indexer

 class Sentence
    {
        string[] words = "The quick brown fox".Split();

        public string this[int wordNum]
        {
            get
            {
                return words[wordNum];
            }
            set
            {
                words[wordNum] = value;
            }
        }
    }

 static void Main(string[] args)
        {
            Sentence se = new Sentence();
            Console.WriteLine(se[3]);
            se[3] = "kangaroo";
            Console.WriteLine(se[3]);
            Console.ReadLine();
        }
static class StaticClass
    {
        static StaticClass()
        {
            Console.WriteLine("This is the static constructor of the static class!");
        }

        public static DateTime DT = DateTime.Now;
    }

Partial class,partial method.

 partial class PaymentForm
    {
       partial void ValidatePayment(decimal amount);
       public void InvokePartialMethod(decimal amount)
        {
            ValidatePayment(amount);
        }
    }

    partial class PaymentForm
    {
       partial void ValidatePayment(decimal amount)
        {
           if(amount>100)
            {
                Console.WriteLine("Luxury");
            }
            else
            {
                Console.WriteLine("Ok");
            }
        }
    }

static void Main(string[] args)
        {
            PaymentForm pf = new PaymentForm();
            pf.InvokePartialMethod(10);
            pf.InvokePartialMethod(101);
            Console.ReadLine();
        }

 

C# 7

标签:star   cube   stat   orm   write   goto   rds   loop   color   

原文地址:https://www.cnblogs.com/Fred1987/p/12238973.html

上一篇:C# 事件系统(二)

下一篇:C# 事件系统(一)


评论


亲,登录后才可以留言!