New features of Visual Studio 2015 and c#6.0More Discovery admin 9 month ago (07-20) 4339 views
Today, we are pleased to announce the new features of Visual Studio 2015 for the next generation of development tools, including: Support for cross-platform C + + development, new Open source. NET compiler platform, C++11 and c++14 support, integration of Apache's Cordova cross-platform mobile development tools and ASP.NET5 new framework, these improvements are small, zero in this excerpt from Visual Studio 2015 's new features in C # development.
1, using the null conditional operator, when invoking the object's properties or methods, we usually need to check whether the object is null, now you do not need to write an if statement, we provide a more convenient way, the following code gets the number of elements in the list, when the list itself is null, The Count method also returns a null value.
public int? GetListItemCount(List<int> list)
{
return list?.Count();
}
2. Use the new nameof keyword to get the string names of parameters, members, and types. One example: When you need to know the string representation of a variable name, we can write:
DateTime currentDate = DateTime.Now;
Console.WriteLine("{0}={1}", nameof(currentDate), currentDate);
The input results are as follows, and the NAMEOF keyword directly outputs the variable name as a string:
CURRENTDATE=2014/12/8 21:39:53
3. Insert the variable directly into the string without having to write the format template in the string format statement such as {n}, like the following code output: "Zero programming, welcome!" ”
String siteName = "zero degree programming";
String welcome = "\{siteName}, welcome!";
4, lambda expression can be used as the implementation of the method body or property, we can directly assign an expression to a method or property.
public class Book
{
public decimal Price { get; set; }
public int Count { get; set; }
public decimal GetTotalPrice() => this.Price * this.Count;
}
public class Book
{
public decimal Price { get; set; }
public int Count { get; set; }
public decimal TotalPrice => this.Price * this.Count;
}
5. Before this, if you want to initialize a property of an object or assign a default value to a property, you need to define a private field that assigns the initial value to the private field, which you can now write:
public class Book
{
public decimal Price { get; set; } = 0m;
public int Count { get; set; } = 10;
}
6, in the new c#6.0 provides a new method of object index initialization, now initialize an object's index can be like this:
Var myObject = new MyObject
{
["Name001"] = "Zero",
["Name002"] = "Programming",
["Name003"] = "Website"
};
7. When catching a catch on an exception, provide a filter to filter the specific exception, where if is a filter, the sample code is as follows:
try
{
throw new ArgumentNullException("bookName");
}
catch (ArgumentNullException e) if(e.ParamName == "bookName")
{
Console.WriteLine("bookName is null !");
}
8, static reference, before this by using the keyword can refer to a namespace, you can now use a reference to a class, through the using reference class, in code can not write the class name, direct access to static properties and methods.
using System.IO.File;
namespace Test
{
public class MyClass
{
public void CreateTextFile(string fileName)
{
CreateText(fileName);
}
}
}
In the code above, CreateText belongs to the method of the file static class under the System.IO namespace, because we introduced the class file with using static, so you can use the method name directly without the need for File.createtext (fileName) to write.
9. This is another anomaly-related feature that allows us to wait for an async method in catch and finally, looking at Microsoft's example:
try
{
res = await Resource.OpenAsync(“myLocation”); // You could do this.
}
catch (ResourceException e)
{
await Resource.LogAsync(res, e); // Now you can do this.
}
finally
{
if (res != null)
await res.CloseAsync(); // … and this.
}
10. Before, struct struct does not allow to write a parameterless constructor, you can now write an parameterless constructor for the struct, when the struct is created by new, the constructor is called, and the constructor is not called when the struct is fetched by default.
11. When you create a project using Visual Studio 2015, you can choose not only the version of the. NET framework, but also the version of the C # language.
12, the initialization of the dictionary, provide a new method, in the new version of C#6 can be used in the following way to initialize a dictionary.
Dictionary<string, string> siteInfos = new Dictionary<string, string>()
{
{ "SiteUrl", "www.xcode.me" },
{ "SiteName", "Zero Programming" },
{ "SiteDate", "2013-12-09" }
};
13, the new code Editor to provide a better experience, the integration of the latest compiler named Roslyn, some new features let you love this editor, the latest in visual Studio 2015 has a new light bulb feature, when there are problems in your code to fix or need to refactor, A small light bulb will appear to the left of the line of code, and the editor will help you automatically repair and refactor the existing code after clicking on the small bulb.
The above is the zero programming for you to organize the visual Studio 2015 in the development of new features, I hope to help you.
New features for Visual Studio 2015 and c#6.0