what's new in c# 6.0

Post on 15-Jul-2015

137 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

Getter-onlyauto-properties(1)

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

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.

Getter-onlyauto-properties(3)

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.

Initializers for auto-properties

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

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.

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.

Using static classes(3)

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.

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.

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?

Expression-bodied methods(2)

Expression-bodied methods(3)

Expression-bodied methods(4)

• This goes for other kinds of function members too .

Expression-bodied methods(5)

Index initializers(1)

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

Index initializers(2)

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

Index initializers(3)

Index initializers(4)

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.

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.

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.

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.

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.

Null-conditional operators(6)

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

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.

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.

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.

Exception filters(1)

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.

Await in catch and finally

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

top related