Probable C # 6.0 features illustrated

Source: Internet
Author: User

Original: http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

===========================================

1. Main constructor

Write a constructor in a shorter way to assign a value to a private variable.

Past
public class Point {    private int x, y;    public Point(intint y)        this.x = x;        this.y = y;    }}
Right now
public class Point(intint y) {    private int x, y;}
Thoughts
    • Do you need to independently define X and Y?
    • Can you still write a body?
    • How would do the default private?

This solution feels too constrained, would has preferred something like:

public Point(set intset int y)

That set the property and optionally created a private one if it didn ' t. Would allow bodies, use on multiple constructors etc.

2. Before automatic attribute assignment
private readonly int x;public intgetreturn x; } }
Right now
public intget; } = x;  
public int XX { get; set; } = xx; 
3. The using static class introduces all public static methods of a class;

Introduces all the public static methods of a class.

Before
public doublegetreturnMath.Sqrt(Math.Round(5.142)); } }
Right now
using System.Math;...public doublegetreturn Sqrt(Round(5.142)); } }
4. Before the property expression
public double Distance {    getreturn Math.Sqrt((X * X) + (Y * Y)); }}
Right now
public double Distance => Math.Sqrt((X * X) + (Y * Y));
Thinking
    • Although the use of the = is actually unrelated to System.Linq.Expression.
5. Before the method expression
public Point Move(intint dy) {    returnnew Point(X + dx1, Y + dy1);}
Right now
public Point Move(intintnew Point(X + dx, Y + dy);
6. The Params parameter supports more types

The previous pararms only supports array.

Before
Do(someEnum.ToArray());...public void Do(params int[] values) { ... }
Right now
Do(someEnum);public void Do(params IEnumerable<Point> points) { ... }
7. Before the null check
ifnull) {    var next = points.FirstOrDefault();    ifnullnullreturn next.X;}   return -1;
Right now
var bestValue = points?.FirstOrDefault()?.X ?? -1;
int? first = customers?[0].Orders?.Count()
8. Constructor type parameter Inference

Removes the need to create static factory methods to infer generic types. This is helpful with tuples etc.

Before
var x = MyClass.Create(1, "X");...public MyClass<T1, T2> Create<T1, T2>(T1 a, T2 b) {    return new MyClass<T1, T2>(a, b);}
After
varnew MyClass(1, "X");
Thoughts
    • Another great addition.
    • Does it understand list and collection initializers to automatically determine the generic types too?
9. Before declaring the output parameter in place
int x;intout x);
Right now
intout int x);
10. More elegant string. Format before
var s = string.Format("{0} is {1} years{{s}} old", p.Name, p.Age);
Right now
var s = "\{p.Name} is \{p.Age} year{s} old";
var s = "\{p.Name, 20} is \{p.Age:D3} year{s} old";
var s = "\{p.Name} is \{p.Age} year\{(p.Age == 1 ? "" : "s")} old";
var s = $"{p.Name} is {p.Age:D3} year{{s}} old";
11. You can use await12 in Catch finally. Before the index initializers of the dictionary
var result = new Dictionary<string, string>()
{

  {"index1", "value1"},
  {"index2", "value2"}
};
Right now
var result = new Dictionary<string, string>()
{
  ["index1"] = "value1",
  ["index2"] = "value2"
};
 

Probable C # 6.0 features illustrated

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.