Essential C# 6 Features You Need to Know!
-
With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.
Don't you feel smarter now?
Quote:
Static Using Syntax
I'm also struggling to see a good use for this. Maybe if you had a huge number of `Console.WriteLine()` statements in a block?
Quote:
Auto-Property Initializers
I like this. Having to move the initialization to the constructor is one of the things I find mildly annoying about properties.
Quote:
Dictionary Initializers
Cleaner looking perhaps, but I don't see how it's supposed to be less error prone.
Quote:
String Interpolation
Meh. Combined with `nameof()` this might make writing a custom `ToString()` for debug purposes a bit better ( `return "{nameof(var1)} = {var1}, {nameof(var2)} = {var2}, ...";` ); but you're supposed to use a different syntax for that instead of abusing `ToString()` anyway.
Quote:
nameOf Expression
You could do this with reflection before but IIRC it was rather fugly. It'd be nice to see if this could make some reflection heavy code I wrote years ago easier to read and less brittle.
Quote:
Expression Bodied Function & Property
Yuck! I'm only not doing my giant X|[^] here for space reasons.
Quote:
Exception Filters
Not sure. His example sucks, but I could see this potentially being useful in other cases.
Quote:
Await in a Catch and Finally Block
Sounds useful but I haven't written any async code yet so not sure.
Quote:
Null Conditional Operator
I've wanted this for years. The example isn't that bad with the old syntax, but try drilling down through 3 or 4 levels of nullable properties/members and checking at each point. Saying elephant it and slapping a try/catch for null reference exceptions was the least unreadable version if not something you'd want to do in hot loops. BEST NEW FEATURE EVER!!!!!!!!!!!!!!!!!!!!!!!!!111oneoneone111elventyone11!11
Did
-
With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.
Don't you feel smarter now?
Michael Crump wrote:
throw new Exception(...
No, no, no, no NO!!! :mad: I wish the authors of the BCL had made the base
Exception
classabstract
, to stop people from doing this. If you want to throw an exception, throw a specific exception type. In this case, it's a problem with an argument beingnull
, so anArgumentNullException
would be appropriate. Throwing the baseSystem.Exception
class is just lazy, and makes your code harder to call - to the extent that Microsoft had to mess with the exception handling[^] to makecatch (Exception)
do the right thing.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Does anyone find "using static" to be helpful? If yes then in what circumstances? I just can't find any reason to use it... even in that very common example ("using static System.Console", which everyone that has posted a C# 6 feature used, I find it's much worse to read it in this manner. There are some nice features, like Auto-Property initializers it could be helpful for creating a readonly properties, but the only extremely helpful thing (at least to me) is the introduction of nameOf...
-
I can´t imagine using it for Console.WriteLine or something, but I think importing extension methods from a class instead of a namespace makes much more sense. Like: using static System.Linq.Enumerable;
-
That's dreadful. X|
PIEBALDconsult wrote:
That's dreadful. X|
The feature, or the sample? :D
-
PIEBALDconsult wrote:
That's dreadful. X|
The feature, or the sample? :D
Yes.
-
I can´t imagine using it for Console.WriteLine or something, but I think importing extension methods from a class instead of a namespace makes much more sense. Like: using static System.Linq.Enumerable;
That's why I separate each of my Extension Methods (with overloads) into its own namespace -- I really can't stand having a whole load of crap brought in when I want only one small piece of crap. But then I also don't like the implementation of Extension Methods requiring the
using
directive, and the C# implementation not directly using the Attribute (unlike VB.net). X| -
I see what you mean, so for example if we have one large Extensions namespace with this we could clarify or emphasise exactly which extensions we are using. Well that could be helpful. Thanks for the reply.
Only as far as the developer allows.
-
That's why I separate each of my Extension Methods (with overloads) into its own namespace -- I really can't stand having a whole load of crap brought in when I want only one small piece of crap. But then I also don't like the implementation of Extension Methods requiring the
using
directive, and the C# implementation not directly using the Attribute (unlike VB.net). X| -
Telerik wrote:
Let’s jump in.
No, let's not.
-
Rob Grainger wrote:
If that continues, we may end up with something as unreadable as Perl.
:laugh: I hate Perl with a vengeance!
Kevin
Kevin McFarlane wrote:
Perl with a vengeance
Isn't that Perl 6? :-D
-
With the final version of C# 6 having been released, we can seen that a lot of syntax changes have occurred since its inception. Because of this, I’ve noticed that most of the blog posts currently on the internet don’t work anymore or are too vague about how they implemented a feature. I thought it would be useful to make a list of the most essential C# 6 features with simple code examples that would make it both easy to understand and simple to copy/paste a sample into a new console app to try it. Let’s jump in.
Don't you feel smarter now?
I used several of this. Then was told my applet had to run on .NET 4.5.1 and had to take them out.
-
I used several of this. Then was told my applet had to run on .NET 4.5.1 and had to take them out.
Most of them are compiler features. So long as you're compiling in VS2015, they should still work even if you're targeting an earlier version of the framework.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Most of them are compiler features. So long as you're compiling in VS2015, they should still work even if you're targeting an earlier version of the framework.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Should and do are two different things.