Visual Studio 2015 series tutorials (1) -- C #6.0 how to use new features

Source: Internet
Author: User
Tags microsoft c

Visual Studio 2015 series tutorials (1) -- C #6.0 how to use new features

This topic of IDE has been in development for a long time and can be traced back to Microsoft. before the NET technology was released, the main product at that time was ActiveX controls. With the rapid rise of Borland Delphi, RAD was unstoppable and quickly gave birth to many classic ides, microsoft's most famous ones are VC and VB. NET technology release, Visual Studio into the 20XX era, for VS2002 and VS2003 I have almost no deep impression, just stay in the memory of this goods to be efficient, you must install a Resharper tool, or it will be a little better than notepad; then Microsoft released VS2005 and VS2008, added the MSBUILD engine, optimized smart tips, and improved the extension model, basically, it is mature and stable. Later, VS2010, which was created in WPF technology, has always been a bloated and gorgeous impression, but lacks the outstanding functions; the two most recent upgrades and releases are VS2012 and VS2013, among which the major points in the headlines are not performance, but Web and cloud.

Microsoft announced with a high profile that Visual Studio 2015 was officially released on July 22, July 20 a few days ago. This was a rapid discussion in the Development Department, almost divided into three schools:

  • Technology stream-I will go. Isn't this IDE something that can be done with a variety of super "notebooks"? Why should I upgrade my skills as long as there is no new technology, it seems that the desktop is dominated by WinForm, and WPF is half-dead. The Web is a big change, but it is just a running environment. In addition, the compilation or msbuild seems to be of little significance.
  • Catch up with the new stream-Haha, Microsoft has a new version. Please download it and try it. This time there will certainly be a lot of killer features, such as C #6.0 and a new Roslyn compilation platform; in addition, asp.net 5.0 is very informative and finally removed system. the dependency of the web is switched to OWIN.
  • Calm down-let these mice get down first. It's really nice to say that this is not necessary for the current task.

In the end, the sound of chasing new streams is quite loud, because everyone is looking forward to the new C # version, although PM Mads Torgersen of the Microsoft C # language group said, "C #6.0 is mainly about enhancing and improving efficiency", but it is still quite promising for the control development team, because code readability and efficiency are a key part of us.

Here, let's take a brief look at the important features of C #6.0:

  1. NameOf expression. Once upon a time, we have been operating on various hardcode parameter exceptions, such:
    Void ThrowArgumentNullException (string firstVersionArgumentName)
    {
    Threw new ArgumentNullException ("firstVersionArgumentName", "can not be null ");
    }
    Sadly, the second version may be said by PM: "This parameter name is not suitable. Let's change it." thanks to the reconstruction function of IDE, this is easy to directly change the name of F2 and press Enter, check in the Code. Several days later, the test came to the door and said that your parameter name has changed, but the exception information has not changed. Well, the original hardcode character group won't change with the refactoring function!
    Let's take a look at what the new Nameof expression brings to me. The code for the same function is as follows:
    Void ThrowArgumentNullException (string firstVersionArgumentName)
    {
    Threw new ArgumentNullException (nameof (firstVersionArgumentName), "can not be null ");
    }
    When you go back to the IDE and press F2 to trigger the refactoring and rename the name, you will find that the exception information can also be changed.
  2. Null-conditional operators (Null-conditional operators), another heavyweight code upgrade, 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 compromise. during the development process, we have countless such methods and repeated null judgments. However, this does not improve the code readability and business processing, it increases Code complexity, making it harder for us to understand the original design intention. Obviously, C #6.0 uses null-conditional operators to take a big step forward:
    Public static string Tuncate (this string value, int length)
    {
    Return value ?. Substring (0, Math. Min (value. Length, length ));
    }
    Is it more concise and clear, and can highlight the core business logic!
  3. The string interpolation function can finally get rid of the long string. Format function. The following code can be easily Rewritten:
    Var fullName = string. Format ("FirstName is {0}, LastName is {1}", customer. FirstName, customer. LastName );
    Code after using the new features:
    Var fullName = "FirstName is \ {customer. FirstName}, LastName is \ {customer. LastName }";
  4. Lambda expression functions and get-only attributes. Functions with only one or two sentences can save some nonsense. This new function can greatly save manpower:
    Public override string ToString () => "\ {FirstName} \ {LastName }";
    Public override int GetHashcode () => (FirstName. GetHashcode () ^ 8) & (LastName. GetHashcode ());
    Public DateTime TimeStamp {get ;}=> DateTime. UtcNow;
  5. Auto-property and Index initializers can finally assign initial values to attributes like variables, greatly improving code readability.
    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 }";
    }
  6. Exception filter (Exception filter), recalling the previous error handling, in order to prompt different errors, we have to define multiple custom exceptions. With the Exception filter, we can solve the problem by adding a simple additional attribute to the exception:
    Try {... }
    Catach (CustomException ex) if (CheckException (ex ))
    {... }
    Think about the other benefit, such as the serious exception log. In this filter, we can make the simplest judgment and find that if it is a serious problem, we can directly make an earlier reminder.
  7. Reference static class (using static) is a must-have for a lazy person. Think about a Daxian defined a Super invincible static class and auxiliary method. you need to use it in many places, then you have to repeat the static class name and method name. If the static class name is too long, it will be even worse. copy it, in the end, I always look at the repetition of the big section and feel very uncomfortable (most programmers have code cleanliness). Well, the static class of this application can be well solved:
    Using GrapeCity. Demo. LongLongNameStaticClass;
    Void AnotherMethod ()
    {
    UtilA (...) // No LongLongNameStaticClass. UtilA (...)
    }
  8. Await enhancement allows you to finally put await into catch and finally blocks. A typical use case is that operations like IO resources can be easily handled and closed:
    Resource res = null;
    Try
    {
    Res = await Resource. OpenAsync (...); // Always do this.
    ...
    }
    Catch (ResourceException ex)
    {
    Await Resource. LogAsync (res, ex); // write logs, no blocking
    }
    Finally
    {
    Res ?. CloseAsync (); // The null-value judgment operator is more concise and clear.
    }

The C #6.0 feature is here. The second article will introduce the content related to code editing and debugging in VS2015 to see how VS 2015 improves efficiency and quality. Please stay tuned!

Visual Studio Code simple trial

Visual Studio 2010 & Help Library Manager installation instructions

How to configure OpenCV 2.3.x/2.4.x in Visual Studio 2005/2008 and Visual Studio 2010

Use the opencv-2.4.0.exe file to compile the x86 or x64 platform Visual Studio 2005/2008/2010 target file

Visual Studio LightSwitch supports HTML5 and JavaScript

Visual Studio 11: use C ++ to develop a simplest Metro application

Visual Studio details: click here
Visual Studio: click here

This article permanently updates the link address:

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.