Resharper has proven to be a great way for me to start learning and integrating new language features. I code in my original style and resharper suggests changes based on new features like pattern matching. At first I didnt see the benefit, but this is a hugely powerful feature once you start to wrap your mind around it.
as an aside, if you got Rider as part of your resharper subscription, try it out, it is basically a drop in replacement for visual studio but faster and with many many little quality of life improvements you never knew you needed :)
C# Pattern Matching
31–40 of 214 posts
Re: C# Pattern Matching
#32Here's an example of the new syntactic sugar: public static double ComputeArea(object shape) { switch (shape) { case Square s: return s.Side * s.Side; case Circle c: return c.Radius * c.Radius * Math.PI; case Rectangle r: return r.Height * r.Length; default: throw new ArgumentException( message: "shape is not recognized", paramName: nameof(shape)); } } The when clause can be used to deal with special cases (e.g. to a…
Re: C# Pattern Matching
#33Earlier quoted context omitted.
With C# 8 you can also write it as: public static double ComputeArea(object shape) => shape switch { Square s => s.Side * s.Side, Circle c => c.Radius * c.Radius * Math.PI, Rectangle r => r.Height * r.Length _ => throw new ArgumentException( message: "shape is not recognized", paramName: nameof(shape)) };
Is `match` a reserved word in C#? I don't use it that much, but have used scala and F# pattern matching and they all use the term `match`, which seems more intuitive.
Re: C# Pattern Matching
#34Earlier quoted context omitted.
public static double ComputeArea(object shape) => shape switch { Square s => s.Side * s.Side, Circle c => c.Radius * c.Radius * Math.PI, Rectangle r => r.Height * r.Length, _ => throw new ArgumentException( message: "shape is not recognized", paramName: nameof(shape)); }
Sorry, I couldn't figure out how to write a code block.
Re: C# Pattern Matching
#35Earlier quoted context omitted.
Every single feature C# got in past years increased my productivity. I understand that it may cause issues to new learners, but overall, what of them do you consider bloat?
Wait till you get to work in some complex code bases and you'll understand why jamming all kinds of idioms into one language can become a disaster. It's a tool after all, if you know how to use it properly you don't paint yourself into a corner. However, too much flexibility can lead to many problems in larger teams. Good luck!
C++ is usually taken as a language with too many features but really it just has a few very flexible features and people abused those features to do all kinds of metaprogramming. As C++ has been adding more native metaprogramming idioms it has actually been getting simpler to code in.
Re: C# Pattern Matching
#36Earlier quoted context omitted.
Is `match` a reserved word in C#? I don't use it that much, but have used scala and F# pattern matching and they all use the term `match`, which seems more intuitive.
It's not reserved (and that's probably why it's not used). Most teams working on languages with a decently long history are very reluctant to add new keywords, because it will break preexisting code. `match` is a very common variable name, not only in relation to regexes. There are of course ways to do contextual parsing so that you can introduce new keywords (I believe this was done in C# with the LINQ extensions),…
In fact, `where` isn't a reserved keyword, either--you can have an identifier named `where`.[1]
To an existing C# programmer, `where` makes a lot of sense, since it's used for matching elsewhere (e.g., LINQ).
[0]: https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
[1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
Re: C# Pattern Matching
#37Is type-sensitive code totally acceptable now? I do use it at times but generally try to avoid it.
Re: C# Pattern Matching
#38Earlier quoted context omitted.
as an aside, if you got Rider as part of your resharper subscription, try it out, it is basically a drop in replacement for visual studio but faster and with many many little quality of life improvements you never knew you needed :)
The spell checker is probably the single greatest feature in Rider. It works really well on composite type names. E.g. 'CustomerPreferences' would not get a squiggle under it for spelling, but 'CusomerPreferences' would. Performance is arguably better in most circumstances, but it doesn't support some of the bleeding edge .NET Core functionality as well as VS2019 does.
Re: C# Pattern Matching
#39Earlier quoted context omitted.
as an aside, if you got Rider as part of your resharper subscription, try it out, it is basically a drop in replacement for visual studio but faster and with many many little quality of life improvements you never knew you needed :)
The spell checker is probably the single greatest feature in Rider. It works really well on composite type names. E.g. 'CustomerPreferences' would not get a squiggle under it for spelling, but 'CusomerPreferences' would. Performance is arguably better in most circumstances, but it doesn't support some of the bleeding edge .NET Core functionality as well as VS2019 does.
Re: C# Pattern Matching
#40Resharper has proven to be a great way for me to start learning and integrating new language features. I code in my original style and resharper suggests changes based on new features like pattern matching. At first I didnt see the benefit, but this is a hugely powerful feature once you start to wrap your mind around it.
as an aside, if you got Rider as part of your resharper subscription, try it out, it is basically a drop in replacement for visual studio but faster and with many many little quality of life improvements you never knew you needed :)