Visual Studio 2015 Express (1)--c#6.0 How to use the new features

Source: Internet
Author: User
Tags finally block microsoft c






The topic of the IDE's controversy has been in the development Department for a long, long-dated and even traceable to Microsoft. NET technology before the release, then the main product is the ActiveX control, with the rapid rise of Borland Delphi, Rad Unstoppable, quickly spawned a lot of classic IDE, Microsoft's most famous is VC and VB, since then. NET technology release, Visual studio into the 20XX era, for VS2002 and VS2003 I have basically not too deep impression, just stay in memory is this goods to high efficiency, must have to install a call ReSharper, otherwise it is stronger than notepad so a little Then Microsoft released VS2005 and VS2008, added the MSBuild engine, optimized smart hints, improved the extended model, basically mature and robust, and then the VS2010 created in WPF technology has been a bloated and gorgeous impression, but the lack of a bright heavy function The last two upgrades and releases are VS2012 and VS2013, and the few major points that are placed on the headlines are not the performance of the web and the cloud.



Microsoft said in a few days that the official release date for Visual Studio 2015 was July 20, which was quickly discussed in the development department, which was divided into three groups:


    • Technology flow--I go, this IDE comes and goes is not so many functions, a variety of super "notepad" can be done, why earn broken head to upgrade, as long as the technology has not changed, seemingly desktop or WinForm-based, WPF dead-end; the web is a big deal, but it's a running environment. , in addition to compiling or MSBuild, it seems that the whole point is small.
    • Chasing new stream--Haha, Microsoft has a new version, fast download to try it, this time there must be a lot of killer features, such as c#6.0, as well as the new Roslyn compiler platform, in addition to the ASP. NET 5.0 is very interesting, and finally take off the reliance of system.web, the whole line instead of Owin.
    • Calm flow-Let them this batch of mice first drip, it is really cool to say, anyway, now the task has not to have this.


Ultimately, the new stream sounds bigger, because everyone is looking forward to the new version of C #, although the Microsoft C # language group's PM Mads Torgersen said, "C # 6.0 is primarily about enhancing and improving efficiency," but for the control development team it is still quite looking forward, Because the readability and efficiency of the code is a very critical part of us.



Here, let's get back together. Briefly review the important features of c#6.0:

An nameof expression. Once upon a time, we have been hardcode various parameter anomalies, such as:
void Throwargumentnullexception (String firstversionargumentname)
{
Threw new ArgumentNullException ("Firstversionargumentname", "Can not is null");
}
Very sad urging is the second edition maybe PM said: "This parameter name is not appropriate, we change it", thanks to the IDE's refactoring function, this is easy, directly F2 renamed and then enter, check in code; After a few days, the test came to the door, saying that your parameter name is changed, but the exception information has not changed. Well, the original HardCode character set, this is not changed with the refactoring function!
Take a look at what the new nameof expression brings me, the same functionality as the code:
void Throwargumentnullexception (String firstversionargumentname)
{
Threw new ArgumentNullException (nameof (firstversionargumentname), "Can Not is null");
}
When you go back to the IDE and press F2 again to trigger a rename, you'll find that the exception information can also be changed together.
Null-valued operator (null-conditional operators), another heavyweight code boost, directly on the sample code:
public static string Tuncate (this string value, int length)
{
if (!string. IsNullOrEmpty (value))
{
return value. Substring (0, Math.min (value. length, length));
}
return value;
}
This is only a small folding, in the development process we have countless such methods, countless repetitions of the empty judgment, but this code readability and business processing without any promotion, but increased the complexity of the code, making it more difficult for us to understand the original design. Clearly, c#6.0 uses null-conditional operators to advance a big step forward:
public static string Tuncate (this string value, int length)
{
return value?. Substring (0, Math.min (value. length, length));
}
is not more concise and clear, but also to highlight the core business logic!
String embedding value (string interpolation), finally can get rid of long string. The Format function, the following code can be easily rewritten:
var fullName = string. Format ("FirstName is {0}, LastName is {1}", customer. FirstName, customer. LastName);
After using the new feature code:
var fullName = "FirstName is \{customer. FirstName}, LastName is \{customer. LastName} ";
Lambda expression functions and get-only properties. For those functions that have only one or two sentences, you can save some nonsense, and this new feature can be very labor-saving:
public override string ToString () = "\{firstname} \{lastname}";
public override int GetHashcode () = (Firstname.gethashcode () ^8) & (Lastname.gethashcode ());
Public DateTime TimeStamp {get;} = Datetime.utcnow;
Automatic attributes (Auto-property) and index initialization (index initializers) can finally give the attribute an initial value like a variable, greatly improving the readability of the code.
public string FirstName {get; set;} = "John";
public string LastName {get; set;} = "Lennon";
Private Dictionary<int, string> _dicts = new Dictionary<int, string> {[3] = "Third", [8] = "Eight"};
public string FullName {get;}
Pubic MyClass ()
{
FullName = "\{firstname} \{lastname}";
}
Exception filter (Exception filter), recall the past error handling, in order to prompt different errors, we have to define a number of custom exceptions, with the exception filter, we can add a simple extra property to the exception can be solved:
try {...}
Catach (Customexception ex) if (Checkexception (ex))
{ ... }
Think of this there is a benefit, such as serious abnormal log, in this filter we can be the simplest judgment, found that if it is a serious problem, you can directly do earlier reminders.
Refer to static class (using static), lazy people must, think of a big fairy in front defined a super Invincible static class and auxiliary method, you have super many places need to use, and then you have to knock over and over again this static class name and method name, in case the static class name is very long more sad, copy it, Finally always look at the big paragraph repeat the heart is very uncomfortable (most programmers have code cleanliness), OK, this application static class can be solved very well:
Using GrapeCity.Demo.LongLongNameStaticClass;
void Anothermethod ()
{
UtilA (...)//No Longlongnamestaticclass.utila (...)
}
await enhancement, you can finally put the await in the catch and finally block, the typical use case is like an IO resource operation can be simple and neat processing closed:
Resource res = null;
Try
{
res = await resource.openasync (...); I've been able to do this all the time.
...
}
catch (Resourceexception ex)
{
Await Resource.logasync (res, ex); Write a log, not block it.
}
Finally
{
Res?. Closeasync (); Combine null to judge operator more concise and clear
}


The c#6.0 feature is here, and in the second we'll look at the VS2015 code editing and debugging related content to see how vs 2015 improves efficiency and quality. Keep your eye on it!



Visual Studio 2015 Express (1)--c#6.0 How to use the new features


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.