zoukankan      html  css  js  c++  java
  • C# 6.0 (VS2015 CTP6)

    /*
    C# 6.0 demo
    https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14
    */
     
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Eventing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json.Linq;
    using static System.Console;  //C# 6.0: Using static members
    using static System.Math; //C# 6.0: Using static members
    using static ConsoleApplication1.MyClass; //C# 6.0: Using static members
    using static ConsoleApplication1.MyStruct; //C# 6.0: Using static members
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                WriteLine("C# 6.0 Demo"); //C# 6.0: Using static members
                WriteLine(RefUrl); //C# 6.0: Using static members
     
                var dictionary = new JObject {["x"] = 3 }; //C# 6.0: Dictionary initializer
                WriteLine("dictionary["x"]: {0}", dictionary["x"]);
     
                var myClass = new MyClass();
                PrintMyClass(myClass);  //C# 6.0: Using static members
                myClass.X = 100;
                PrintMyClass(myClass);
     
                var anotherDist = myClass.AnotherMyClass?.Dist; //C# 6.0: Null propagation
                WriteLine("myClass.AnotherMyClass?.Dist: {0}", anotherDist);
                // anotherDist == null
                //true
                //anotherDist > 0
                //false
                //anotherDist == 0
                //false
                //anotherDist < 0
                //false
                //anotherDist = 0
                //0
                //anotherDist
                //0
                myClass.AnotherMyClass = new MyClass(y: 0);
                anotherDist = myClass.AnotherMyClass?.Dist;
                WriteLine("myClass.AnotherMyClass?.Dist: {0}", anotherDist);
     
                string s = nameof(Console.Write); //C# 6.0: nameof operator
                WriteLine("nameof(Console.Write) = {0}", s);
     
                var p = new {Name = "Bob", Age = 30};
                WriteLine($"{p.Name} is {p.Age - 10} years old."); //C# 6.0: String interpolation
     
                try
                {
                    throw new Exception("hi!");
                    throw new Exception("wah!");
                }
                catch (Exception e) when (e.Message == "hi!") //C# 6.0: Exception filters
                {
                    WriteLine("Catch a message is "hi!" exception. ");
                }
                catch (Exception e)
                {
                    WriteLine("Catch other message exception. Message is "{0}"", e.Message);
                }
     
     
                Write("Press any key to EXIT...");
                ReadKey(true);
            }
        }
     
         
        public struct MyStruct
        {
            public static string RefUrl = "https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14";
        }
     
     
        public class MyClass
        {
            public int X { get; set; } = 90; //C# 6.0: Auto-property initializers
     
            public int Y { get; } = 100; //C# 6.0: Getter-only auto-properties
     
            public MyClass(int y)
                :this()
            {
                Y = y; //C# 6.0: Ctor assignment to getter-only autoprops
            }
     
            public MyClass()
            {
                 
            }
     
            public double Dist => Sqrt(X * X + Y * Y); //C# 6.0: Expression-bodied members
     
            public MyClass AnotherMyClass { get; set; }
     
            public static void PrintMyClass(MyClass myClass)
            {
                WriteLine($"PrintMyClass: X = {myClass.X}, Y = {myClass.Y}, Dist = sqrt(x*x+y*y) = {myClass.Dist}");
            }
        }
     
    }
    

    结果:

    C# 6.0 Demo
    https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14
    dictionary["x"]: 3
    PrintMyClass: X = 90, Y = 100, Dist = sqrt(x*x+y*y) = 134.536240470737
    PrintMyClass: X = 100, Y = 100, Dist = sqrt(x*x+y*y) = 141.42135623731
    myClass.AnotherMyClass?.Dist:
    myClass.AnotherMyClass?.Dist: 90
    nameof(Console.Write) = Write
    Bob is 20 years old.
    Catch a message is "hi!" exception.
    Press any key to EXIT...
    
  • 相关阅读:
    nginx端口重定向及反向代理
    Linux Crontab实现定时备份和删除Docker中的Mysql数据库
    Linux创建定时任务
    Docker可视化管理工具Portainer的安装配置及使用
    Centos磁盘扩容-免重启
    使用CSS让网页自适配手机端
    Centos7 安装FTP
    Centos7 部署的Redis无法远程访问
    移动端调试
    select2初始化默认选中值(多选)
  • 原文地址:https://www.cnblogs.com/Bob-wei/p/4515045.html
Copyright © 2011-2022 走看看