C # 6 New Features

C # 6 New Features

There is a new C # compiler in C # 6. It is complete source code cleanup. Custom programs can now also use the characteristics of the compiler pipeline, and use many of the features of Visual Studio.
This new platform allows C # compiler improvements C # with many new features. Although these features are not async keyword LINQ or large impact, but many improvements have increased developer productivity. C # 6 What has changed?

0.3.1 Static using statement

C# 5

using System;

Console.WriteLine("Hello,World!");

C# 6

using static System.Console;

WirteLine("Hello,World!");

0.3.2 Expression body method

Body expression method includes only a statement can be written using lambda syntax:

C#5

public bool IsSquare(int num)
{
     return num == 0;
}

C#6

public bool IsSquare(int num) => num == 0;

0.3.3 Expressions body property

The method is similar to the expression thereof, only the one-way property may get accessor lambda syntax written:

C#5

public string FullName
{
	get
	{
	 	return FirstName + "  " + LastName;
	}
}

C#6

public string FullName => FirstName + "  " + LastName;

0.3.4 automatically attribute initializer

C#5

public class Person
{
	public Person()
	{
		Age = 24;
	}
	public int Age {get; set;}
}

C#6

public class Person
{
	public int Age {get; set;} = 42;
}

0.3.5 Automatic Read-Only attribute

In order to achieve the read-only attribute, C # 5 requires full attribute syntax; in C # 6, the attributes can be used to automatically:

C#5

private readonly int _bookId;
        public int BookId {
            get
            {
                return _bookId;
            }
        }

C#6

 public int BookIdTest { get; }

0.3.6 nameof operator

Nameof the new operator, may access the field name, attribute name, type name or method name. Thus, in the reconstruction, it will not change the name of the missing:

C#5

public void Method(object o)
{
	if (o == null) throw new System.ArgumentNullException("o");
}

C#6

public void MethodTest(object o)
{
	if (o == null) throw new System.ArgumentNullException(nameof(o));
}

0.3.7 null propagation operator

Null propagation operators simplify inspection null value:

C#5

int? age = p = null ? null : p.Age;

C#6

int? age = p?.Age;

C#5

var handler = Event;
if(handler != null)
{
	handler(source,e)
}

C#6

handler?.Invoke(source,e)

0.3.8 string interpolation

String interpolation delete a call to string.Format, which is not used in the string format numbered placeholders, placeholders may contain expressions:

C#5

public override ToString()
{
	return string.Format("{0},{1}",Title,Publisher)
}

C#6

public override ToString() => #"{Title}  {Publisher}"

0.3.9 Dictionary initializer

Dictionary Dictionary can now be initialized to initialize, similar to collection initializers.

C#5

var dict = new Dictionary<int,string>();
dict.Add(3,"three");
dict.Add(7,"seven");

C#6

var dict = new Dictionary<int,string>()
{
	[3] = "three",
	[7] = "seven"
};

0.3.10 exception filter

Abnormal filter allows the filter before they catch exceptions.

C#5

try
{

}
catch(MyException ex)
{
	if(ex.ErrorCode != 405) throw;
}

C#6

try
{

}
catch(MyException ex) when (ex.ErrorCode == 405)
{

}

One advantage of the new syntax is that it not only reduces the length of the code, and there is no change in the stack trace ---- C # 5 will change the stack trace.

0.3.11 Catch the await

C#5

bool hasError = false;
string errorMessage = null;
try
{

}
catch (System.Exception ex)
{
    hasError = true;
    errorMessage = ex.Message;
}
if (hasError)
{
    await new MessageDialog().ShowAsync(errorMessage);
}

C#6

try
{

}
catch (System.Exception ex)
{
	await new MessageDialog().ShowAsync(errorMessage);
}
Published 62 original articles · won praise 5 · Views 3935

Guess you like

Origin blog.csdn.net/qq_42194657/article/details/102917962