Live data from Hacker News

C# Pattern Matching

docs.microsoft.com

1–10 of 214 posts

Re: C# Pattern Matching

#2
Such a beautiful, elegant language. Simple to grasp and lets the compiler handle all that legitimate complexity.

Wish more language designers would respect code authors like these designers do.

Re: C# Pattern Matching

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

Re: C# Pattern Matching

#5
post #4

Is type-sensitive code totally acceptable now? I do use it at times but generally try to avoid it.

It should be avoided, but there are legitimate use-cases. Deserialization, for example, might yield a few different types. Especially if you don't control the endpoint and the data back is a mess.

When it is used, it's very nice to have help from the language. I much prefer that to some kind of purist, "they shouldn't do that so we won't help". I certainly miss having type guards when I work in Java.

Re: C# Pattern Matching

#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 avoid division by 0 when a dimension is 0).

Re: C# Pattern Matching

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

Re: C# Pattern Matching

#9
post #4

Is type-sensitive code totally acceptable now? I do use it at times but generally try to avoid it.

C# as standard uses camelCase for internal variables and UpperCamelCase for public variables, properties and methods.

https://docs.microsoft.com/en-us/dotnet/standard/design-guid...

Re: C# Pattern Matching

#10

Pattern matching is so much better in F#. C# gets more and bloated to the point of paralysis. It's not yet there but I;m sure it will at some point.

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?

Post reply on HN