what's new in c# 6

63
WHAT’S NEW IN C# 6

Upload: amir-barylko

Post on 08-Jan-2017

409 views

Category:

Software


0 download

TRANSCRIPT

WHAT’S NEW IN C# 6

STRING INTERPOLATION

No Interpolation

Concatenation

String.Format

$”Hello {var1}”

C#6.0

$”Value of {price:C2}”

C#6.0

NULL CONDITIONAL OPERATOR

Checking for null

Is a drag

Don’t check

Shortcircuit to null

perhapsNull?.FullName()

C#6.0

USING STATIC

Static methods have to be always

qualified

Enumerable.Empty<T>

using static Enumerable;

C#6.0

Empty<T>

C#6.0

AUTOPROPERTIES INITIZALITION

Getter and Setter declaration

Initialize in the constructor

string Name {get; set;} = “Harry”

C#6.0

GETTER ONLY INITIALIZATION

Getter declaration

Initialize in constructor

string Name {get;} = “Harry”

C#6.0

EXPRESSION BODIES ON METHODS

Methods always need a body

No matter how short

void Method() => expr

C#6.0

EXPRESSION BODIES ON PROPERTIES

Properties always need a body

No matter how short

void Property => expo

C#6.0

NAMEOF EXPRESSIONS

Using hardcoded strings to refer to

expressions

Is error prone

Wrong spelling

Variable changes

nameof(var.FirstName)

C#6.0

EXCEPTION FILTERS

Similar to VB.NET and F#

Exception catch block can have a guard

try {…} catch (Exception e) when (expr) { … }

C#6.0

INDEX INITIALIZER

Simplify dictionary initialization

var numbers = new Dictionary<int, string> { [7] = "seven", [9] = "nine", [13] = "thirteen" };

C#6.0

AWAIT ON CATCH/FINALLY

BLOCKS

Similar to F#

Catch and Finally blocks can have Await

calls

Resource res = null; try { res = await Resource.OpenAsync(…); … } catch(ResourceException e) { await Resource.LogAsync(res, e); } finally { if (res != null) await res.CloseAsync(); }

C#????

PRIMARY CONSTRUCTORS

Simplify constructor declaration

public class Post(long postId, string name, string title){ public long PostID { get; } = postId; public string PostName { get; } = name; public string PostTitle { get; } = title; }

C#????

INDEXED PROPERTY OPERATOR

Simplify Dictionary initialization

dict[“Harry”]

dict.$Harry

C#????

THANK YOU!

The Smartest tool for agile project management

http://smartview.io

[email protected]

@abarylko

http://bit.ly/abarylkoslides

http://orthocoders.com

http://westerndevs.com

Resources

• Github Wiki C# 6 language features: http://bit.ly/Csharp6_features

• MSDN C# 6 features: http://bit.ly/csharp6_msdn

• C# 6.0 in six screenshots: http://bit.ly/csharp_screenshots

Photo Credit

• Under http://creativecommons.org/licenses/by/2.5/

• Jeremy Keith, Roast beef, http://flic.kr/p/TKUz

• Rob Campbell, Field of daisies, http://flic.kr/p/6QJjU4