New features of Visual Studio 2015 and c#6.0

Source: Internet
Author: User



Original link: http://www.xcode.me/more/visual-studio-2015-new-features






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 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 degree",
["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.



New features of Visual Studio 2015 and c#6.0


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.