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 ...
随机推荐
- Testlink安装成功后首页提示“There are security warning for your consideration.”
Testlink安装成功后,登录Testlink,首页显示警告信息: “There are security warnings for your consideration. See details ...
- unity里c# gc优化 -字符串
1使用unsafe,直接修改字符串 public static class UnsafeString { public static unsafe void Copy(this string str, ...
- 我的Android进阶之旅------>Android项目运行报java.lang.NoClassDefFoundError错误的解决办法
今天在运行一个Android项目的时候,报了以下错误: D/AndroidRuntime( 3859): Shutting down VM E/AndroidRuntime( 3859): FATAL ...
- 使用Compute Engine工具连接Linux VM
Links: Connecting to Linux Instances 内容: 要连接Linux VM实例,必须要有一个SSH(Secure Shell)秘钥.无论何时连接一个LinuxVM实例(通 ...
- IP地址处理模块IPy
IP地址规划是网络设计中非常重要的一个环节,规划的好坏会直接影响路由协议算法的效率,包括网络性能.可扩展性等方面. 在这个过程中,免不了要计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP ...
- MQ,互联网架构解耦神器
一个架构常识:当调用方需要关心执行结果,通常使用RPC调用. ret = PassportService::userAuth(name, pass); switch(ret){ case(YES) ...
- imx6q 添加intel PCIE网卡
TQ_IMX6Q开发板移植rtl8168-PCIE转千兆网卡 一.配置内核选项PCIE总线驱动支持 默认的内核配置可能没有把PCIE的总线驱动编入内核,所以需要确认是否把驱动编译到了内核里面. 配置好 ...
- Qt事件机制---信号通过事件实现,事件可以过滤,事件更底层,事件是基础,信号是扩展。
转:http://www.cnblogs.com/findumars/p/8001484.html Qt事件机制(是动作发生后,一种通知对象的消息,是被动与主动的总和.先处理自己队列中的消息,然后再处 ...
- 防止iframe被别的网站引用
try{ top.location.hostname; if (top.location.hostname != window.location.hostname) { top.location.hr ...
- INSPIRED启示录 读书笔记 - 第34章 恐惧、贪婪、欲望
消费者购买产品大多源于情感需求 企业级消费者出于恐惧和贪婪购买产品:如果不买这款产品,竞争对手会超过我,黑客会攻破我的防火墙,客户将弃我而去:如果买了,会赚得更多,省得更多 大众消费者购买产品的原因更 ...