/*
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)的更多相关文章

  1. 使用Qt5.7.0 VS2015版本生成兼容XP的可执行程序 good(从VS2012 update1开始支持xp和c++11)

    一.直接使用VS2012/VS2013/VS2015生成XP兼容的可执行程序 Visual Studio刚发布时没打补丁,称为RTM版,之后会陆续发布补丁,进行bug修复和功能增强.VS2010及之前 ...

  2. 使用Qt5.7.0 VS2015版本生成兼容XP的可执行程序

    版权声明:本文为灿哥哥http://blog.csdn.net/caoshangpa原创文章,转载请标明出处. 一.直接使用VS2012/VS2013/VS2015生成XP兼容的可执行程序 Visua ...

  3. opencv3.2.0+opencv_contrib-3.2.0+vs2015开发配置

    在sift算法实现中,出现了这个问题 出现问题:\vs2015opencv\vs2015\project\mode\mode\sift算法1.cpp(3): fatal error C1083: 无法 ...

  4. #一周五# VS2015 CTP6, TFS2015 CTP1更新,老衣的开发工具汇总,2015 MVP 社区巡讲

    又到周五,这一周博主我工作效率极高,每天更新博客一篇,<快速创建网站>系列已经进程大半了,希望这个系列能够对大家有所帮助.今天周五了,博主要休息一下,就给大家唠叨一下这段时间都发生了什么. ...

  5. opencv3.0+VS2015+64位win7配置

    首先,我们把所有用到的工具下载下来 VS2015         看这里,http://news.mydrivers.com/1/439/439398.htm,我下载的是专业版. OPENCV3.0 ...

  6. Windows8.1 + Nvidia cuda8.0 + Vs2015

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Vs2015 1.查看本机配置,查看显卡类型是否支持NVIDIA GPU选中计算机-->右键属性-->设备管理 ...

  7. WIN10-64+CUDA8.0+OpenCV3.0+VS2015配置

    在网上参考了各位前辈的攻略后,捣鼓了两天总算配置成功了. 目前网上多的还是ubuntu上配置CUDA的教程比较多比较详尽,但是我现在就是想在windows10上跑啊,就是想用CUDA啊... 1.CU ...

  8. #一周五# VS2015 CTP6, TFS2015 CTP1更新,老衣的开发工具汇总,2015 MVP 社区巡讲

    又到周五,这一周博主我工作效率极高,每天更新博客一篇,<快速创建网站>系列已经进程大半了,希望这个系列能够对大家有所帮助.今天周五了,博主要休息一下,就给大家唠叨一下这段时间都发生了什么. ...

  9. OpenCV3.2.0+VS2015开发环境配置

    vs2015安装可参考:https://www.jianshu.com/p/391e67529bd3 OpenCV3.2.0配置可参考:https://www.jianshu.com/p/026093 ...

随机推荐

  1. SpringMVC流程架构图

    [组件说明] 以下组件通常使用框架提供实现: 1.DisPatcherServlet:前端控制器(不需要程序员开发) 用户请求到达前端控制器,它相当于MVC模式中的C(Controller),Disp ...

  2. jenkins配置SVN报错

    jenkins配置SVN报错,如图:

  3. linux下tcpdump命令的使用

    一般情况下linux系统会自带tcpdump工具,如果系统没有安装,直接用命令安装就行了. 安装命令:yum install -y tcpdump 查看安装版本命令:tcpdump --help 查看 ...

  4. 基于Cpython的 GIL(Global Interpreter Lock)

    一 介绍 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...

  5. python基础:while循环,for循环

    ---恢复内容开始--- 1.使用while循环输出1 2 3 4 5 6     8 9 10 2.求1-100的所有数的和 3.输出 1-100 内的所有奇数 4.输出 1-100 内的所有偶数 ...

  6. hbase中清空整张表的数据

    hbase(main):005:0> truncate 'fr:test' Truncating 'FaceBase' table (it may take a while): - Disabl ...

  7. msql 2000 使用DBCC CHECK DB 得出错误,槽引用错误

    转自:http://www.cnblogs.com/firstrose/p/4256257.html 某个SQL2000的数据库,在通过备份/还原的方法升级到2005时发生错误: 查找解决方法未果 正 ...

  8. Ajax知识

    一.Ajax介绍前戏 1.json介绍 1.什么是json? json(JavaScript Object Notation,JS对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript ...

  9. 插入排序 Insertion Sort

    插入排序算法的运作如下: 通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. 插入排序算法的实现我放在这里. 时间/空间复杂度: 最差时间复杂度 O(n^2) 最优时间 ...

  10. C# 截屏函数

    截屏函数: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; ...