Quantcast
Channel: Marcel Meijer Blog
Viewing all articles
Browse latest Browse all 70

Visual Studio 2015 Preview: C# 6

$
0
0

On November 12 and 13 Microsoft organized the Connect(); event in New York. the keynote was live and the Dutch communities (WAZUG, DotNed, VB Central and of course SDN) had organized a evening event to watch the keynote together. If you weren’t there or missed the event completely, you can still watch via Channel 9. Not the least speakers were there: Scott Guthrie, S. Somasegar, Brian Harry and Scott Hanselman.

During this event Microsoft showed the words of CEO Satya Nadella, Mobile First, Cloud First, weren’t just a hollow remark. The statement is part of whole Microsoft and in all the do. The keynote showed it.

They did a lot of announcements, one was the fact Microsoft made the .NET platform Open Source via GitHUB. Which means anyone can do a Pull request and solve bugs etc. Microsoft will pick up these changes (after testing etc.) corporate in the branch. How cool is that! The first pull request came within a hour after opening.

An other announcement was a preview of Visual Studio 2015. Besides that also new previews of ASP vNext and C# 6. However there are no new language elements added, but the language and tooling is cleaned and optimized. All adjustments are meant to make it easier for the developer.

In this blogpost a few of the changes.

Code must besides easier to read also cleaner. Take this simple method, we could do without the function body.

11-22-2014 15-57-00

By using the Lambda arrow (=>) it is possible to make it shorter. The Lambda arrow was already in the language, but now you can use it for this too. There is an other usage.

11-22-2014 15-57-00

Clean code, understandable and readable. Self documenting code Knipogende emoticon

Other change. You will recognize the next code construction.

11-22-2014 16-00-57

With the new Elvis operator it can be shorter and more readable. The ?. (image some Elvis hair and eyes) operator checks for the object to be NULL.

11-22-2014 16-01-07

Also posisble with methods in a Class: x?.Calc(1,2); Personally I find this dangerous, because the Calc methode is not executed if x is NULL. And if your tests or structure of the application is not good, this will unpredictable situations.

Another construction you and I use a lot. We make strings for messages and we use of course the string.Format method. Super easy and rather readable, disadvantage you need to count the position and it is not always clear in a blink of an eye.

11-22-2014 16-03-39

So this should be different. Simple \{<variable>} and the code is more documented. Of course the formatting rules of string.Format are still possible, complete freedom for the text.

11-22-2014 16-03-39

The above line makes the variable a field of a class/object. By making it public, you can manipulate it. You better use properties. But nowadays properties need to have a getter and a setter. In C# 6 there is the notion of a read-only property, a property with a getter and no setter.

This in the above example done with the lambda arrow, it will transfer in a read-only property automatically. You see this by the Code lens reference counter. But if you want to change the property in code you get a compiler warning.

11-22-2014 16-04-47

11-22-2014 16-01-54

There are lots more, more about that later.


Viewing all articles
Browse latest Browse all 70

Trending Articles