what's new in c# 6.0

35
What's New In C# 6.0

Upload: learningtech

Post on 15-Jul-2015

139 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: What's new in c# 6.0

What's New In C# 6.0

Page 2: What's new in c# 6.0

Design philosophy

No big new concepts In C# 6 as we call it, we haven’t added any sort of big new concepts

that you have to wrap your head around.

Many small features

Clean up your code It’s more a list of small useful features that help clean up your code

and get rid of the boilerplate that gets in the way of expressing your intent clearly.

Page 3: What's new in c# 6.0

Getter-onlyauto-properties(1)

• Currently auto-properties need to have setters and this kind of puts immutable data types at a disadvantage.

Page 4: What's new in c# 6.0

Getter-onlyauto-properties(2)

‘Class.property.get’ must declare a body because it is not marked abstract or extern.Automatically implemented properties must define both get and set accessors.

Page 5: What's new in c# 6.0

Getter-onlyauto-properties(3)

Page 6: What's new in c# 6.0

Getter-onlyauto-properties(4)

• They have a back end field that’s read only and just like read only fields they can be assigned from the constructor.

• That doesn’t call a setter, it just assigns directly to the back end field.

Page 7: What's new in c# 6.0

Initializers for auto-properties

• We also allow initializers in our properties in the same way as we do for fields.

Page 8: What's new in c# 6.0

Using static classes(1)

• Using clauses we’re let those refer to static classes, not just to namespaces and so what this does is to put the static members of the class directly into scope.

Page 9: What's new in c# 6.0

Using static classes(2)

• So if you look at this static method call, for instance you don’t need to prefix it with a class name anymore.

• You can just call that square root method directly.

Page 10: What's new in c# 6.0

Using static classes(3)

Page 11: What's new in c# 6.0

String interpolation(1)

• String.Format, very versatile but this business with the placeholder number, it kind of gets confusing.

• It’s error prone and frankly it muddles your intent.

Page 12: What's new in c# 6.0

String interpolation(2)

• It would be much better if the values that you’re formatting in so to speak, if they occurred in their place.

Page 13: What's new in c# 6.0

Expression-bodied methods(1)

• For Lambda expressions we already give you the option to use a single express rather than the whole statement body.

• So why not do the same for methods?

Page 14: What's new in c# 6.0

Expression-bodied methods(2)

Page 15: What's new in c# 6.0

Expression-bodied methods(3)

Page 16: What's new in c# 6.0

Expression-bodied methods(4)

• This goes for other kinds of function members too .

Page 17: What's new in c# 6.0

Expression-bodied methods(5)

Page 18: What's new in c# 6.0
Page 19: What's new in c# 6.0

Index initializers(1)

• Today you can initialize properties in an object initializer but index setters still have to be assigned in separate statements.

Page 20: What's new in c# 6.0

Index initializers(2)

• You can now assign to index inside object initializers so that J Object can be initialized in just one expression.

Page 21: What's new in c# 6.0

Index initializers(3)

Page 22: What's new in c# 6.0

Index initializers(4)

Page 23: What's new in c# 6.0

Null-conditional operators(1)

• First we check the json itself is not null before index into it.• Then we check that the result of the indexing is not null before we

access its type twice.

Page 24: What's new in c# 6.0

Null-conditional operators(2)

• So null conditional operators are featured to make this null checking kind of fade into background.

• If the left-hand side is null, the whole thing is null.• And only if the left-hand side is not null then we do the dot and that becomes

the result of the operation.

Page 25: What's new in c# 6.0

Null-conditional operators(3)

• Indexing can be made null-conditional too.• You just insert a question mark in front of the square brackets and

now they only happen if receiver is not null.

Page 26: What's new in c# 6.0

Null-conditional operators(4)

• A great use of these null-conditional operators is triggering events.• Today you trigger an event like this, but first you have to check if it’s null.

Page 27: What's new in c# 6.0

Null-conditional operators(5)

• But this way of checking isn’t threat safe because someone might unsubscribe the last event handler just after you checked and boom.

Page 28: What's new in c# 6.0

Null-conditional operators(6)

• So you need to copy the delegate reference first before you check and before you trigger.

Page 29: What's new in c# 6.0

Null-conditional operators(7)

• You just conditionally call the invoke method on the delegate and it will get called only if the delegate isn’t null.

Page 30: What's new in c# 6.0

The nameofoperator(1)

• Quite often you need to get the name of the program element as a string like with point here.

• The problem with that is that if you rename point you’re likely to forget changing the string which is now out of sync.

Page 31: What's new in c# 6.0

The nameofoperator(2)

• So the name of operator just let you get that string while checking that the program element exists and what it binds to, so if you do a rename re-factoring, for instance, the string changes too.

Page 32: What's new in c# 6.0

Exception filters(1)

Page 33: What's new in c# 6.0

Exception filters(2)

• Allow a catch block to filter its exceptions before actually catching them.

• This better than catching and re-throwing because when you re-throw an exception you lose the information about where it originally occurred so downstream you get a worse exception.

Page 34: What's new in c# 6.0

Await in catch and finally

Page 35: What's new in c# 6.0

References https://channel9.msdn.com/Events/Visual-Studio/Connect-event-2014/116