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

C# 6.0 (VS2015 CTP6)

时间:2015-05-19 18:53:23      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:

/*
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...

 

C# 6.0 (VS2015 CTP6)

标签:

原文地址:http://www.cnblogs.com/Bob-wei/p/4515045.html

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