A brief introduction to the new features of C #6. net 6 is coming ?,

Source: Internet
Author: User
Tags finally block

A brief introduction to the new features of C #6. net 6 is coming ?,

A few weeks ago I read some new features related to C #6 in different places. I decided to collect them all together. If you haven't read them, you can repeat them all at once. Some of them may not be as magical as expected, but it is only the current update.

You can download VS2014 or install the Roslyn package for visual studio2013 to obtain them.

Let's take a look:

1. $ identifier

$ Is used to simplify string indexes. It has different dynamic characteristics from those in C # That use regular expression matching to implement index. Example:

var col = new Dictionary<string, string>()
            {
                $first = "Hassan"
            };

   //Assign value to member
   //the old way:
   col.$first = "Hassan";

   //the new way:
   col["first"] = "Hassan";
2. Exception Filter

The VB compiler already supports exception filters. Now C # supports exception filters. The exception filter allows developers to create a catch Block for a condition. The code in the catch block is executed only when the condition is met. This is one of my favorite features, for example:

try
            {
                throw new Exception("Me");
            }
            catch (Exception ex) if (ex.Message == "You")
3. catch and finally block await keywords

No one knows why the await keyword in the catch and finally code blocks in C #5 is unavailable, regardless of the writing method. This is good because developers often want to view the I/O operation logs. in order to record the caught exception information to the logs, asynchronous operations are required.

try
            {
                DoSomething();
            }
            catch (Exception)
            {
                await LogService.LogAsync(ex);
            }
4. Declaration expression

This feature allows developers to define a variable in an expression. This is simple but practical. In the past, I used asp.net to do a lot of websites. below is my common code:

long id;
if (!long.TryParse(Request.QureyString["Id"], out id))
{ }

Optimized Code:

if (!long.TryParse(Request.QureyString["Id"], out long id))
{ }

In this Declaration method, the scope of the variable is the same as that of C.

5. Static usage

This feature allows you to specify a specific type in a using statement. After that, all static members of this type can be used in subsequent clauses.

using System.Console;

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            //Use writeLine method of Console class
            //Without specifying the class name
            WriteLine("Hellow World");
        }
    }
}
6. Automatic attribute initialization:

C #6 Automatic comfort attributes are like the domain at the declared position. The only thing you need to know here is that this initialization will not cause the setter method to not be called internally. The Domain value in the background will be directly set. The following is an example:

public class Person
    {
        // You can use this feature on both
        //getter only and setter / getter only properties

        public string FirstName { get; set; } = "Hassan";
        public string LastName { get; } = "Hashemi";
    }
7. Main constructor:

Calling Haha, the main constructor will help you eliminate the pain of getting the constructor parameters and setting them to the class domain to support subsequent operations. This is really useful. The main purpose of this feature is to use the constructor parameters for initialization. When the main constructor is declared, all other constructor must use this () to call this main constructor.

The following is an example:

//this is the primary constructor:
    class Person(string firstName, string lastName)
    {
        public string FirstName { get; set; } = firstName;
        public string LastName  { get; } = lastName;
    }

Who should introduce Object C? The simpler the better?

Objective-C, usually written in ObjC and rarely used in Objective C or Obj-C, is an object-oriented programming language that expands C. It is mainly used in Mac OS X and GNUstep systems that use the OpenStep standard. Objective-C is a very practical language. It is a very small Runtime Library written in C, Objective-C purpose: Write IOS operating systems (such as iphone, ipod touch, ipad, and other Apple mobile terminal devices) application tool. The popularity of Objective-C is attributed to the success of the iphone, because Objective-C has been used to write iphone applications.

Introduction to simple C language Algorithms

Step 1: p = 1, Step 2: I = 2, step 3: p * I should be assigned to p, so p * I = 1*2 is assigned to p
That is, p = 1*2. Step 4: Assign I + 1 to I. At this time, I = 3. Step 5: Determine whether I is greater than five, no return to Step 3: Assign p * I to p, so p * I = 1*2*3 is assigned to p
That is, the current p = 1*2*3, Step 4: Assign I + 1 to I, at this time I = 4, step 5: Determine whether I is greater than five, no return to Step 3: Assign p * I to p, so p * I = 1*2*3*4 is assigned to p
That is, the current p = 1*2*3*4, Step 4: Assign I + 1 to I, at this time I = 5, Step 5: Determine whether I is greater than five, no return to Step 3: Assign p * I to p, so p * I = 1*2*3*4*5 is assigned to p
That is, the current p = 1*2*3*4*5, Step 4: Assign I + 1 to I, at this time I = 6, Step 5: judge whether I is greater than five, yes, and end. In this case, p = 1*2*3*4*5 = 5!
In this way, I + 1 is prepared to take 3, 4, and 5 next to it!

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.