码迷,mamicode.com
首页 > Windows程序 > 详细

C# 7

时间:2020-01-28 23:05:07      阅读:101      评论:0      收藏:0      [点我收藏+]

标签: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(i<=5)
            {
                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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!