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...
C# 6.0 (VS2015 CTP6)的更多相关文章
- 使用Qt5.7.0 VS2015版本生成兼容XP的可执行程序 good(从VS2012 update1开始支持xp和c++11)
一.直接使用VS2012/VS2013/VS2015生成XP兼容的可执行程序 Visual Studio刚发布时没打补丁,称为RTM版,之后会陆续发布补丁,进行bug修复和功能增强.VS2010及之前 ...
- 使用Qt5.7.0 VS2015版本生成兼容XP的可执行程序
版权声明:本文为灿哥哥http://blog.csdn.net/caoshangpa原创文章,转载请标明出处. 一.直接使用VS2012/VS2013/VS2015生成XP兼容的可执行程序 Visua ...
- opencv3.2.0+opencv_contrib-3.2.0+vs2015开发配置
在sift算法实现中,出现了这个问题 出现问题:\vs2015opencv\vs2015\project\mode\mode\sift算法1.cpp(3): fatal error C1083: 无法 ...
- #一周五# VS2015 CTP6, TFS2015 CTP1更新,老衣的开发工具汇总,2015 MVP 社区巡讲
又到周五,这一周博主我工作效率极高,每天更新博客一篇,<快速创建网站>系列已经进程大半了,希望这个系列能够对大家有所帮助.今天周五了,博主要休息一下,就给大家唠叨一下这段时间都发生了什么. ...
- opencv3.0+VS2015+64位win7配置
首先,我们把所有用到的工具下载下来 VS2015 看这里,http://news.mydrivers.com/1/439/439398.htm,我下载的是专业版. OPENCV3.0 ...
- Windows8.1 + Nvidia cuda8.0 + Vs2015
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Vs2015 1.查看本机配置,查看显卡类型是否支持NVIDIA GPU选中计算机-->右键属性-->设备管理 ...
- WIN10-64+CUDA8.0+OpenCV3.0+VS2015配置
在网上参考了各位前辈的攻略后,捣鼓了两天总算配置成功了. 目前网上多的还是ubuntu上配置CUDA的教程比较多比较详尽,但是我现在就是想在windows10上跑啊,就是想用CUDA啊... 1.CU ...
- #一周五# VS2015 CTP6, TFS2015 CTP1更新,老衣的开发工具汇总,2015 MVP 社区巡讲
又到周五,这一周博主我工作效率极高,每天更新博客一篇,<快速创建网站>系列已经进程大半了,希望这个系列能够对大家有所帮助.今天周五了,博主要休息一下,就给大家唠叨一下这段时间都发生了什么. ...
- OpenCV3.2.0+VS2015开发环境配置
vs2015安装可参考:https://www.jianshu.com/p/391e67529bd3 OpenCV3.2.0配置可参考:https://www.jianshu.com/p/026093 ...
随机推荐
- JQuery UI 入门
1. JQuery UI 概述 1.1 JQuery UI 主要分为三部分: 交互部件(interactions):是一些与鼠标交互相关的内容; 小部件(widgets): 主要是一些页面的扩展; 效 ...
- MySQL优化器不使用索引的情况
优化器选择不适用索引的情况 有时候,有乎其并没有选择索引而去查找数据,而是通过扫描聚集索引,也就是直接进行全表的扫描来得到数据.这种情况多发生于范围查找.JOIN链接操作等情况.例如 ; 通过SHOW ...
- KVM WEB管理工具webvirtmgr安装和使用
生产环境的KVM宿主机越来越多,需要对宿主机的状态进行调控.这里用webvirtmgr进行管理.图形化的WEB,让人能更方便的查看kvm 宿主机的情况和操作 1 安装支持的软件源 yum -y ins ...
- ./bin/console server:run Surprise! There are no commands defined in the "server" namespace.
Let's start the built-in web server: ./bin/console server:run Surprise! There are no commands defi ...
- Linux中vim命令出现E325错误解决方法
出现该问题的原因是: vim在编辑文件的时候会创建一个swp file来保证文件的安全性,如果没有正常退出vim的,下次打开这个文件就会报E325的错误,提示swp文件已经存在. 解决方法也简单:把这 ...
- How do I resolve the CodeSign error: CSSMERR_TP_NOT_TRUSTED?
How do I resolve the CodeSign error: CSSMERR_TP_NOT_TRUSTED? The Xcode build error titled "CSSM ...
- [转] - 学习ASP.NET比较完整的流程!
如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 掌握一门.NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.N ...
- linux mint —— 图片一张
概述 Linux Mint是一種基於Ubuntu開發出的Linux操作系统.由Linux Mint Team团队于2006年开始发行.Linux Mint 的目标是为家庭用户和企业客户提供一个免费.高 ...
- Apache的order、allow、deny
Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 所以,最常用的是:Or ...
- Hyperledger Fabric1.0环境搭建
一.准备CentOS系统,本文使用的是CentOS7.0 二.安装Docker 执行命令 yum -y install docker 验证是否安装成功 docker --version 三.安装Doc ...