标签:
VS 2015中已经包含C# 6.0. C#在发布不同版本时,C#总是会有新特性,比如C#3.0中出现LINQ,C#4.0中的动态特性,c#5.0中的异步操作等。.
C# 6.0中与增加了不少新的特性,帮助开发人员更好的编程。
下面的示例需要下载vs2015,这样才会有C#6.0环境,主要的新特性有:
using System;
using static System.Console;
namespace CSharp6FeaturesDemo
{
class Program
{
static void Main(string[] args)
{
WriteLine("This is demo for C# 6.0 New Features");
ReadLine();
}
}
}
public class Employee
{
public Guid EmployeeId { get; set; } = Guid.NewGuid();
public string FirstName { get; set; } = "Mukesh";
public string LastName { get; set; } = "Kumar";
public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
}
Dictionary<int, string> myDictionary = new Dictionary<int, string>()
{
[1] = "Mukesh Kumar",
[2] = "Rahul Rathor",
[3] = "Yaduveer Saini",
[4] = "Banke Chamber"
};
Console.WriteLine($"The Full Name of Employee {firstName} {lastName}");
C#6.0中,可以使用lambda表达式在一行内定义一个简单的函数,而不是传统的需要定义一个完整的函数。
6.0之前6.0中public static string GetFullName(string firstName, string lastName)
{
return string.Format("{0} {1}", firstName, lastName);
}public static string GetFullName(string firstName, string lastName) => firstName + " " + lastName;
string FirstName { get; } = "Mukesh";
try
{
throw new Exception(errorCode.ToString());
}
catch (Exception ex) when (ex.Message.Equals("404"))
{ WriteLine("This is Http Error"); }
catch (Exception ex) when (ex.Message.Equals("401"))
{ WriteLine("This is Unathorized Error"); }
catch (Exception ex) when (ex.Message.Equals("403"))
{ WriteLine("Forbidden"); }
//No need to check null in if condition
//null operator ? will check and return null if value is not there
WriteLine(employees.FirstOrDefault()?.Name);
//set the default value if value is null
WriteLine(employees.FirstOrDefault()?.Name ?? "My Value");
static void Main(string[] args)
{
if (int.TryParse("20", out var result)) {
return result;
}
return 0; // result is out of scope
}
具体内容和示例代码可参考:
标签:
原文地址:http://www.cnblogs.com/margiex/p/5132949.html