Some New Features in C# 6.0
来源:互联网 发布:软件验收测试包括 编辑:程序博客网 时间:2024/05/04 05:36
Static Using Syntax
In previous versions of C#, we would need to add the proper using statement, such as System, then we could write the following line of code:
- Console.WriteLine("Hello Abrar!");
With C# 6, you can now add the using statement and reference the WriteLine method by itself as in the following:Auto-Property Initializers
In the past, we may have created our properties with a get and set and then initialized our constructor with the value that we wanted as shown below.
- using System.Console;
- namespace Console1
- {
- class Program
- {
- static void Main(string[] args
- {
- //Sample One
- WriteLine("Hello Abrar!");
- }
- }
- }
In the past, we may have created our properties with a get and set and then initialized our constructor with the value that we wanted as shown below.
- public class Customer
- {
- public Customer()
- {
- customerID = Guid.NewGuid();
- }
- public Guid customerID
- {
- get;
- set;
- }
- }
We can now reduce this code block to one line as shown below. No longer do we need to create a setter or constructor.
- public class Customer
- {
- public Guid customerID { get; set; } = Guid.NewGuid();
- }
Dictionary Initializers
Many C# developers felt the format shown below was confusing for creating dictionaries, mainly because of the use of brackets and quotation marks for the data.The compiler team decided to change this and make it more readable with the following format. This new format will only save you a few keystrokes, but it is much easier to read in my opinion.Exception Filters and Async in a Catch and Finally Block
Exception Filters
Exception filters have been supported in Visual Basic, but are new to the C# compiler. They allow you to specify a condition for a catch block. As shown in the following sample, the last catch statement will fire.Async in a Catch and Finally Block
I think many developers will love this feature because they often need to log exceptions to a file or database without blocking the current thread. Here is an example of how one would work:Name of Expressions
How many times in your code do you retrieve the name of a member of your class? You would typically wrap the field in quotation marks as shown below.The main problem with this is that it forces you to put strings in your code to represent the name of a member. In C# 6.0, there is a new operator called nameof that will allow us to refactor our code to remove these string literals. The sample shown below will show how we could refactor that same method.This results in cleaner code and safety when retrieving member names.
String Interpolation
Prior to C# 6.0, we typically concatenate two or more strings together in one of the following ways:In C# 6.0, we have a cleaner format as shown in the first WriteLine call, but we can also put expressions directly in the string literal to evaluate an expression as shown below:
Many C# developers felt the format shown below was confusing for creating dictionaries, mainly because of the use of brackets and quotation marks for the data.
- Dictionary<string, string> customerNames = new Dictionary<string, string>()
- {
- { "Abrar Ahmad", ".net" },
- { "Yogesh", "Java" },
- { "Ganesh", "SQL" }
- };
- public Dictionary<string, string> customerNames = new Dictionary<string, string>()
- {
- ["Abrar Ahmad "] = ". net ",
- ["Yogesh"] = "Java",
- ["Ganesh "] = " SQL "
- };
Exception Filters
Exception filters have been supported in Visual Basic, but are new to the C# compiler. They allow you to specify a condition for a catch block. As shown in the following sample, the last catch statement will fire.
- public static async void DownloadAsync()
- {
- try
- {
- throw new Exception("Error");
- } catch
- {
- await Task.Delay(2000);
- WriteLine("Waiting 2 seconds");
- }
- finally
- {
- await Task.Delay(2000);
- WriteLine("Waiting 2 seconds");
- }
- }
I think many developers will love this feature because they often need to log exceptions to a file or database without blocking the current thread. Here is an example of how one would work:
- public static async void DownloadAsync()
- {
- try
- {
- throw new Exception("Error");
- }
- catch
- {
- await Task.Delay(2000);
- WriteLine("Waiting 2 seconds");
- }
- finally
- {
- await Task.Delay(2000);
- WriteLine("Waiting 2 seconds");
- }
- }
How many times in your code do you retrieve the name of a member of your class? You would typically wrap the field in quotation marks as shown below.
- public static void DoSomething(string name)
- {
- if (name != null) throw new Exception("name");
- }
- public static void DoSomething(string name)
- {
- if (name != null) throw new Exception(nameof(name));
- }
String Interpolation
Prior to C# 6.0, we typically concatenate two or more strings together in one of the following ways:
- string firstName = "Abrar";
- string lastName = "Ahmad";
- WriteLine("Name : " + firstName + " " + lastName);
- WriteLine(string.Format("Name : {0} {1}", firstName, lastName));
- string firstName = "Abrar";
- string lastName = "Ahmad";
- WriteLine("Name : \{firstName} \{lastName}");
src: By Abrar Ahmad Ansari onApr 10, 2015
http://www.c-sharpcorner.com/UploadFile/d06872/some-new-features-in-C-Sharp-6-0/
0 0
- Some New Features in C# 6.0
- Discuss some new features in WCF 4.0
- New Features in C# 3.0
- New features in C# 2.0
- Some New in C# 3.0 language
- New C# Features in the .NET Framework 4
- C++ 11/14 some new features
- New Features in Xcode 4.3
- New Features in Spring 3.0
- New Features in Xcode 6
- Visual C# 2005 New features
- New Features in EJB3.1(Part 1)
- New features in EJB3.1 (Part 2)
- New features in EJB3.1 (Part 3)
- New features in EJB3.1 (Part 4)
- New Features in HSQLDB 1.9 and 2.0
- New Features in C++11 学习
- 55 New Features in JDK 7 - NIO2
- 进制
- No update on April 11 and 12, 2015!
- MySQL数据库学习&整理(二)数据类型与操作数据表
- map的4种遍历方法
- 技术的看法
- Some New Features in C# 6.0
- java集合框架面试
- Java设计模式之状态模式
- 常用算法之排序算法三【快速排序】
- How to Check Whether API Server is up or Down
- MCSDK(TI-DSP多核软件开发套件)安装配置
- Vim的分屏功能
- webstorm与eclipse快捷键比较
- leetcode 第27题 Remove Element