Live data from Hacker News

C# Pattern Matching

docs.microsoft.com

31–40 of 214 posts

Re: C# Pattern Matching

#31
post #7
post #3

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 :)

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

#32
post #6

Here'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…

Its cool but I would call it switch-by-class-type instead of pattern matching.

Re: C# Pattern Matching

#33

Earlier 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.

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), but it complicates things for all eternity, so you want to avoid it.

Re: C# Pattern Matching

#34

Earlier 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.

Two spaces in front of each line

Re: C# Pattern Matching

#35

Earlier 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!

Java proves that limiting language features for "simplicity" just pushes the complexity somewhere else and generally much worse.

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

#36
post #33

Earlier 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),…

C# isn't too hesitant to introduce new keywords because it supports contextual keywords.[0] Many C# keywords are also valid identifiers. In this case, using `match` in place of `where` probably wouldn't have introduced any incompatibilities.

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

#38
post #31
post #7

Earlier 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.

It also doesn't support some dried up crusty .NET functionality like webforms either (it compiles it fine, but doesn't have the code generation tools that VS has )

Re: C# Pattern Matching

#39
post #31
post #7

Earlier 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.

Resharper spell checks in VS for me too.

Re: C# Pattern Matching

#40
post #7
post #3

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 :)

Does Rider work as a XAML designer yet?
Post reply on HN