C# Pattern Matching
docs.microsoft.com
C# Pattern Matching
1–10 of 214 posts
Re: C# Pattern Matching
#2Wish more language designers would respect code authors like these designers do.
Re: C# Pattern Matching
#3Re: C# Pattern Matching
#4Re: C# Pattern Matching
#5Is type-sensitive code totally acceptable now? I do use it at times but generally try to avoid it.
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 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
#7Resharper 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
#8Re: C# Pattern Matching
#9Is type-sensitive code totally acceptable now? I do use it at times but generally try to avoid it.
https://docs.microsoft.com/en-us/dotnet/standard/design-guid...
Re: C# Pattern Matching
#10Pattern 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.
I understand that it may cause issues to new learners, but overall, what of them do you consider bloat?