C# 6 Features
-
This thread[^] is sparking some debate in the new section, but we should probably do that here, right? So, the first thing is notice is that I find myself really hating string interpolation with a passion. Anything in a string should be string, not some sneaky variable name. C# is not PHP. Await in catch/finally sounds great though. In the past I've needed ugly hacks to approximate that.
* Static initializers - I can't imagine using it for Console.WriteLine or something (we always type cw{Tab} anyway, right?) And there's a problem that some methods don't have an intuitive name without the class name, like
Tuple.Create
. But I like that I can now import extension methods from classes instead of namespaces. * Auto property initializers - Very neat. Should have been there from the beginning. * Dictionary initializers - I guess it's a more intuitive way of writing this particular code but was it absolutely necessary? No. Now we just have 2 slightly different ways of writing the same thing. * String interpolation - I agree it doesn't look pretty, but the old way ({0},{1},{2},etc) didn't look pretty either. * nameof() - Very small feature, but it makes sense to use it everywhere you can. * Expression-bodied members - I think this is a confusing feature. It doesn't even save that much typing. Methods don't look like methods anymore and properties no longer have a 'get' keyword. * Exception filters - Should be useful on rare occasions, but will be abused more often. * Await in catch/finally - Without it, async/await is an incomplete feature, so it should be there, but I don't expect to use it much. * Null conditional - This takes away a lot of ugly, boring, annoying code. Very nice. But it will make C# more cryptic for novice programmers. -
The null check will be dead useful, not sure if I'd adopt much of the other stuff. But then I still for "foreach" like the dinosaur I am.
F-ES Sitecore wrote:
I still for "foreach" like the dinosaur I am
No! Use
for
whenever you can! Avoidforeach
and its ilk unless it's the only thing that will work. -
F-ES Sitecore wrote:
I still for "foreach" like the dinosaur I am
No! Use
for
whenever you can! Avoidforeach
and its ilk unless it's the only thing that will work. -
This thread[^] is sparking some debate in the new section, but we should probably do that here, right? So, the first thing is notice is that I find myself really hating string interpolation with a passion. Anything in a string should be string, not some sneaky variable name. C# is not PHP. Await in catch/finally sounds great though. In the past I've needed ugly hacks to approximate that.
Static Using Syntax - I think its usefulness really depends on the use-case. If you have a class that makes heavy use of
Console
or static members inMath
, it can be beneficial; otherwise, things might get weird easily. Auto-Property Initializers - I don't use much of the syntactic sugar since C# 3.0, but it's good for people who like it because this feature makes auto properties complete. Dictionary Initializers - I don't really care. String Interpolation - Looks better than theString.Format
syntax but can be problematic when it comes to localization. Still, I like it a lot. nameof() Expression - A small but very welcome feature, I use it everywhere I can. This one really shines in Roslyn-based refactorings. Expression-bodied Methods and Properties - I don't really care, not too beneficial. Exception Filters - I guess this one has been supported by the CLR ever since, now it is exposed by C#. Can be useful, but like many others I see a large abuse vector. Await in Catch/Finally - I don't do much async stuff, but it makes the whole feature complete. Null-Conditional Operator - Surely a nice to have shortcut, but it makes the code harder to read, especially for people who are not used to it. I think this version of C# brought some very welcome additions, but it's also beginning to get seriously bloated. Too much choices and syntactic sugar can become problematic and a mess if you're working on projects with different kinds of coding guidelines. -
Not as efficient as a
for
for the same task. -
Not as efficient as a
for
for the same task.OK, but that may be a bit outdated - for example, the difference between private static int test1(int[] ar) { int sum = 0; foreach (var i in ar) { sum += i; } return sum; } and private static int test2(int[] ar) { int sum = 0; for (int i = 0; i < ar.Length; i++) { sum += ar[i]; } return sum; } Is .. not much. The loop bodies are
_loop1:
mov eax,dword ptr [rdx+r9+10h]
add ecx,eax
inc r8d
add r9,4
cmp r8d,r10d
jl _loop1_loop2:
mov eax,dword ptr [rdx+r9+10h]
add ecx,eax
inc r8d
add r9,4
cmp r8d,r10d
jl _loop2Literally the same thing, it even used the same registers.
-
harold aptroot wrote:
What's wrong with foreach?
The young'uns prefer using linq for everything these days. Apparently unreadable, single-line, non-debugabble code using slow features like anonymous methods is the way to go :)
-
OK, but that may be a bit outdated - for example, the difference between private static int test1(int[] ar) { int sum = 0; foreach (var i in ar) { sum += i; } return sum; } and private static int test2(int[] ar) { int sum = 0; for (int i = 0; i < ar.Length; i++) { sum += ar[i]; } return sum; } Is .. not much. The loop bodies are
_loop1:
mov eax,dword ptr [rdx+r9+10h]
add ecx,eax
inc r8d
add r9,4
cmp r8d,r10d
jl _loop1_loop2:
mov eax,dword ptr [rdx+r9+10h]
add ecx,eax
inc r8d
add r9,4
cmp r8d,r10d
jl _loop2Literally the same thing, it even used the same registers.
Now try the
for
in reverse. And try it with something more complex than an array ofint
s. -
Now try the
for
in reverse. And try it with something more complex than an array ofint
s. -
foreach
is more general in that it can work on anyIEnumerable
, whereasfor
is more general in that it doesn't require anIEnumerable
. ::shrug:: I find thatfor
, particularly a reversefor
(which reduces calls to Length or Count or whatever), can perform much better thanforeach
. Though the actual time saved may not be significant, still you don't know what else is going on in the system. On why to use DataViews[^]