C# 6.0 (VS2015 CTP6)

來源:互聯網
上載者:User

標籤:

/*C# 6.0 demohttps://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 membersusing static System.Math; //C# 6.0: Using static membersusing static ConsoleApplication1.MyClass; //C# 6.0: Using static membersusing 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 Demohttps://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14dictionary["x"]: 3PrintMyClass: X = 90, Y = 100, Dist = sqrt(x*x+y*y) = 134.536240470737PrintMyClass: X = 100, Y = 100, Dist = sqrt(x*x+y*y) = 141.42135623731myClass.AnotherMyClass?.Dist:myClass.AnotherMyClass?.Dist: 90nameof(Console.Write) = WriteBob is 20 years old.Catch a message is "hi!" exception.Press any key to EXIT...

 

C# 6.0 (VS2015 CTP6)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.