/*
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. Python文件操作——逐行插入内容

    需求: 分析: 1)打开文件 2)readlines() 读文件(按行读) --> 拿到文件内容 data 3)循环遍历 data 的每行,在每行后面进行拼接要插入的内容 4)往新文件里写 代码 ...

  2. 我的Android进阶之旅------>Android中通过adb shell input来模拟滑动、按键、点击事件

    今天在维护公司的一个小项目的时候,发现按公司手机的某个物理按键,激活相应的Service后,会在屏幕上模拟的点击了屏幕的某个坐标点.好家伙,原来是之前该项目的版本是按这个物理按键后,会弹出一个对话框, ...

  3. 卷积神经网络(CNN)的训练及代码实现

    本文部分内容来自zouxy09的博客.谢谢.http://blog.csdn.net/zouxy09/article/details/9993371 以及斯坦福大学深度学习教程:http://ufld ...

  4. Could not create ServerSocket on address 0.0.0.0/0.0.0.0:9083

    遇到这种情况大家都找不到头绪,是因为你开始运行了hive的metastore,可以输入jps 然后出现如下: 红线所示就是hive metastore的进程 为了重新启动,需要把这个进杀掉: kill ...

  5. iOS 根据农历日期 获取当前的农历年份 即 干支纪年法算农历年

    前言:我国古代是用干支纪年的,近代史上提到的甲午战争.戊戌变法.辛亥革命等名词就是干支纪年.所谓干支就是十天干和十二地支的简称.天干.地支按照一定规则(单配单,双配双)可以搭配成60对,也就是一个甲子 ...

  6. 每天一个Linux命令(48)ping命令

        ping命令用来测试主机之间网络的连通性.     (1)用法:     用法:  ping [参数] [主机名或IP地址]     (2)功能:     功能:  确定网络和各外部主机的状态 ...

  7. 配置树莓派3和局域网NTP服务器实现内网时间校准

    一.配置局域网NTP服务器 1.安装ntp-4.2.8p5-win32-setup.exe 下载地址:https://www.meinbergglobal.com/english/sw/ntp.htm ...

  8. node拦截器设置

    node的拦截器主要目的是用户登录的时候为用户存了一个session,用户登录后的其他操作都要经过拦截器,对比session的值,并把session的过期时间延长. 拦截器主要是在路由文件routes ...

  9. jquery图片过滤归类应用

    在线演示 本地下载

  10. QGIS 编译

    QGIS 编译 在编译的过程中花费了很长时间,特别是编译Debug版本.release版本的编译可以从晚上找到很多的资料,但是Debug的编译相对较少.在Debug编译的过程中,需要单独build工程 ...