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...
时间: 2025-01-04 05:35:17

C# 6.0 (VS2015 CTP6)的相关文章

QT5.6.0+VS2015编译MQSQL(ACCESS)X64数据库驱动

QT5.6.0+VS2015编译MQSQL(ACCESS)数据库驱动 1 说明 l 不建议QT5.6.0使用ACCESS数据库.如果想使用轻量级的数据库,可以使用Sqlite数据库. QT想要访问Access.SQL Server等数据库可以通过ODBC的方式来访问,但是QT较高版本已不提供ODBC驱动库,需要自己编译.QT5.6.0编译出来的数据库驱动是64位,但是不建议安装Access数据库,微软本身也不建议安装64位office. PS:为什么QT5.6.0不编译32位数据库驱动:编译数据

opencv3.0 vs2015属性表

<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ImportGroup Label="PropertySheets" /> <PropertyGroup La

在win10下实现openCV3.2.0+vs2015+cmake出错解决方案

在研究sift算法时发现opencv3.X版本中有些库函数被去掉了.网上查了一下,确实没有sift算法这部分代码了,需要通过网上下载附加库opencv_contrib-3.2.0进行cmake编译就可以用了,于是进入漫长的cmake编译之旅!! 1.网上下载cmake 进入网站下载一个cmake,这里我下的是当前网站上的最新版本,个人觉得这个对编译影响不大.进入界面下载如下方式: 这里选择: Windows win64-x64 Installer: Installer tool has chan

OpenCV3.2.0+opencv_contrib-3.2.0+VS2015+cmake

这篇文章是安装OpenCV3.2.0的扩展库opencv_contrib-3.2.0的介绍. 一.先说使用的配置环境及准备工作: 1.OpenCV-3.2.0-vc14 2.opencv_contrib-3.2.0下载:https://github.com/opencv/opencv_contrib/releases 3.vs2015 4.cmake下载:http://www.cmake.org/,点右上角的Download,我选的是cmake-3.8.0-win64-x64.zip(免安装):

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

又到周五,这一周博主我工作效率极高,每天更新博客一篇,<快速创建网站>系列已经进程大半了,希望这个系列能够对大家有所帮助.今天周五了,博主要休息一下,就给大家唠叨一下这段时间都发生了什么. 转自ANB:http://anb.io/blog/anbfriday150306/ Visual Studio 2015 CTP6 2015年了,Visual Studio 也要发2015版了.从去年发布了免费的Visual Studio Community (社区版)后,Visual Studio越来越向

0 VS2015 WIN7 配置OPENGL

原文链接 OpenGL环境配置 FreeGLUT 和 Glew FreeGLUT: 第三方库,可以用来显示窗口,管理用户输入,以及执行一些其他操作. GLEW:跨平台第三方库,可以简化获取函数地址的过程,并且包含了可以跨平台使用的一些其他OpenGL编程方法. 本文采用添加FreeGLUT和GLEW的库文件到我们项目下自己建的一个目录,然后在VS中配置项目. (1).准备资源: OpenGL配置.rar (2).新建一个VS项目,具体步骤如下: 添加源文件*.cpp (3).解压"OpenGL配

OpenCV学习笔记(一)——OpenCV3.1.0+VS2015开发环境配置

摘要: 由于最近AR(增强现实)这个概念非常火爆,各种基于AR的应用及游戏逐渐面向大众,而在AR中最重要的两个技术就是跟踪识别和增强渲染,其中跟踪识别是通过OpenCV这个开源的计算机视觉库来实现的,所以我就想着研究一下这个库,这里是个人的学习笔记,不是什么权威的教程,如果你们有错误也麻烦帮我指出哈. =============================================分割线==================================================

OpenCV学习笔记(一)——OpenCV3.3.0+VS2015开发环境配置

前言: 什么是OpenCV?可能还有人不清楚吧,简单地说,OpenCV--Open Source Computer Vision Library,即开源计算机视觉库,它是基于C语言和部分C++语言来开发,可用于计算机视觉.图像处理以及模式识别和跟踪. 一.准备工作: 1.下载OpenCV安装包: 到OpenCV的官网(http://opencv.org/)下载最新版本的OpenCV安装包,由于OpenCV针对不同平台都有安装程序,所以我们只需要根据当前开发环境选择合适的平台版本即可,这里我们是在

C#6.0 VS2015

https://msdn.microsoft.com/en-us/library/hh156499(v=vs.140).aspx This page lists key feature names for each version of C# with descriptions of the new and enhanced features in the lastest version of the language. Previous Versions C# 1, Visual Studio