Sometimes it feels like we're entering a new age of coding. C# is so full of "features" that code refactoring tools like ReSharper, CodeRush and JustCode became simply indispensable. It is quite hard to be sure, every time, what is the most elegant/compact way of doing things. In the .Net/VisualStudio world, the language tooling (Intelisense, visual drawing XAML & WinForms, etc) became as important as the language se…
For the last decade or so the c# world has been moving away from those ill conceived tools. Winforms/Webforms were drag'n'drop based and have since been replaced with xaml and html. SSDT has given way to migration based approaches. Intellisense has become it's own lightweight thing and consumable from more tools, you can code just fine without it. ReSharper I've never used, I find it gets in my face more than it help…
What’s New in C# 7.0
131–140 of 278 posts
Re: What’s New in C# 7.0
#132Having done a lot of python recently, this looks great (and familiar). But some things feel a little bit rushed: - In the example "(o is int i || (o is string s && int.TryParse(s, out i))": When reading this statement as a human, o is obviously not an int when it comes down to the TryParse function. But if the 1st part was removed, the 2nd part wouldn't be valid either. I know this is technically how declarations wor…
There is precedence for this in the form of GetEnumerator and GetAwaiter.
Re: What’s New in C# 7.0
#133"Switch statements with patterns
We’re generalizing the switch statement so that:
•You can switch on any type (not just primitive types)
•Patterns can be used in case clauses
•Case clauses can have additional conditions on them
Here’s a simple example:
switch(shape)
{
case Circle c:
WriteLine($"circle with radius {c.Radius}");
break;
case Rectangle s when (s.Length == s.Height):
WriteLine($"{s.Length} x {s.Height} square");
break;
case Rectangle r:
WriteLine($"{r.Length} x {r.Height} rectangle");
break;
default:
WriteLine("");
break;
case null:
throw new ArgumentNullException(nameof(shape));
}There are several things to note about this newly extended switch statement:
•The order of case clauses now matters: Just like catch
clauses, the case clauses are no longer necessarily disjoint, and the first one that matches gets picked. It’s therefore important that the square case comes before the rectangle case above. Also, just like with catch clauses, the compiler will help you by flagging obvious cases that can never be reached. Before this you couldn’t ever tell the order of evaluation, so this is not a breaking change of behavior.
•The default clause is always evaluated last: Even though the null case above comes last, it will be checked before the default clause is picked. This is for compatibility with existing switch semantics. However, good practice would usually have you put the default clause at the end.
•The null clause at the end is not unreachable: This is because type patterns follow the example of the current is expression and do not match null. This ensures that null values aren’t accidentally snapped up by whichever type pattern happens to come first; you have to be more explicit about how to handle them (or leave them for the default clause).
Pattern variables introduced by a case ...: label are in scope only in the corresponding switch section."
Re: What’s New in C# 7.0
#134I don't like the idea of accessing tuple values by their local name var names = LookupName(id); WriteLine($"found {names.first} {names.last}."); It's leaky and should simply use the deconstructing syntax instead. I'm on the fence about the ref returns. It can lead to some fantastic performance improvements and enable otherwise unusable datastructures. But is C# really a language you want if that is important to you.…
C# is used by hundreds if not thousands of games by ways of Unity and is therefore demonstrably good enough in this scenario. Furthermore, just because a language is not C++ does not mean you shouldn't take every opportunity to give programmers tools to write fast code - at the end of the day I doubt anyone would be interested in a language that is purposefully slow.
Performance was a core feature of the language (yes, yes, it's managed) from day 1 with unsafe - it's nice to see further improvements in that department.
Use C++ when you need to, definitely, but when you're copying about 256 bytes around for a trivial world-view-projection matrix multiplication[1] you have a problem.
[1]: https://github.com/dotnet/roslyn/issues/10378#issuecomment-2...
Re: What’s New in C# 7.0
#135Earlier quoted context omitted.
For the last decade or so the c# world has been moving away from those ill conceived tools. Winforms/Webforms were drag'n'drop based and have since been replaced with xaml and html. SSDT has given way to migration based approaches. Intellisense has become it's own lightweight thing and consumable from more tools, you can code just fine without it. ReSharper I've never used, I find it gets in my face more than it help…
SSDT style database upgrades are still the most popular. Since it's what most of the red gate tools do, unless you use ready roll.
Re: What’s New in C# 7.0
#136Earlier quoted context omitted.
The Midori project proved otherwise. One of the reasons we are stuck with C and C++ is because other language vendors, including Microsoft, dropped the ball regarding performance of type safe languages. Do you think C++ would have been kept around if Java and the CLR had been AOT compiled to native code with focus on compiler optimisations since day one? I really appreciate the efforts of the .NET team in adopting th…
Some things can be rather tricky to AOT-compile, especially when you don't have a fixed "world" (i.e. when code can be dynamically loaded, as is the case with both Java classes and CLR assemblies). Consider virtual generic methods, for example. If your set of types is not statically bound, you can't allocate the appropriate number of vtable slots in advance, because you don't know all the instantiations.
I was already using dlls with plugins in Windows 3.1 and C++.
Re: What’s New in C# 7.0
#137Earlier quoted context omitted.
"WinForms" is "language tooling"? "XAML" is "language tooling"? By what definition? I can write server-side C# in vim (without omnisharp) just fine, actually. Maybe the tools became "indispensable" because they add value? I refactor my code in vim but it would probably be faster in VSCode. That doesn't make C# a bad language.
C# has (had? I'm still a couple of versions behind) so much syntactical overhead that writing code without a visual studio seems painful at the very least. The var keyword was the first time it was even possible to write C# without tooling. It has definitely made it possible for me to use Vim for editing C# code. Newer features, such as lambda expressions, and some of the much nicer property syntax makes me feel that…
I always thought of C# as the improved, less verbous Java. I wouldn't compare it with Python or other dynamic languages, but I prefer it to most compiled languages (especially at the times it was created)
Re: What’s New in C# 7.0
#138Earlier quoted context omitted.
One I've come across is binding a command to a button. With anonymous inner classes you can have something like: myButton.AddListener(new Command { bool enabled() { return false; //more logic goes here } void onClick() { //do something } });
I'm not convinced that gives you that much more over using a delegate.
Re: What’s New in C# 7.0
#139Pattern matching! Tuples! Awesome, C# 7.0 is scratching two itches I've felt every time I've worked with it. If only .NET had a suitable cross-platform UI toolkit, then I'd be using it everywhere. Eto.Forms is a good attempt but I found rather buggy and limited at this point in development. Avalonia, while further along in terms of stability and features, doesn't even try to fit in with any platform's look and feel.
Why not winforms? Very convenient, and mostly works in mono
Re: What’s New in C# 7.0
#140Earlier quoted context omitted.
Can you give an example of how C# would be different with anonymous classes? Googling that term just gives a whole lot of references to inner classes in Java.
One I've come across is binding a command to a button. With anonymous inner classes you can have something like: myButton.AddListener(new Command { bool enabled() { return false; //more logic goes here } void onClick() { //do something } });