Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored CS0077 Example with Modern C# Pattern Matching #43108

Merged
merged 6 commits into from
Oct 18, 2024

Conversation

shethaadit
Copy link
Contributor

@shethaadit shethaadit commented Oct 17, 2024

Summary

PR Description:

This PR introduces a significant improvement to the existing example for resolving CS0077, showcasing the transition from the traditional as operator to modern C# pattern matching (is) for value types.

While the as operator is traditionally used for safe casting of reference types, applying it to non-nullable value types, such as structs, results in the CS0077 compiler error. This update replaces the outdated approach with cutting-edge pattern matching techniques, fully embracing the capabilities of C#'s modern syntax.

Key Highlights:

  • Replaced unsafe use of as on a value type with pattern matching using is.
  • Demonstrated how value types should be handled in casting scenarios using the type-checking and assignment capabilities of is.
  • Retained the use of as for reference types, ensuring that both paradigms (reference vs. value types) are illustrated within the same example.
  • This update is fully aligned with current best practices, ensuring developers adopt cleaner, more efficient, and safer code when handling type conversions.

By leveraging modern C# features, this refactor not only resolves the outdated issue but also elevates the educational value of the example, leading to more robust, readable, and maintainable code in future C# projects.

Fixes #42861


Internal previews

📄 File 🔗 Preview link
docs/csharp/misc/cs0077.md docs/csharp/misc/cs0077

@shethaadit shethaadit requested review from BillWagner and a team as code owners October 17, 2024 17:22
@dotnetrepoman dotnetrepoman bot added this to the October 2024 milestone Oct 17, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates PR is created by someone from the .NET community. label Oct 17, 2024
@BartoszKlonowski
Copy link
Contributor

@shethaadit Why submitting a PR to an issue that already has a comment saying someone is working on it?
Even if some time has passed since the comment, it is a good practice to ask that person whether it can be taken.

@shethaadit
Copy link
Contributor Author

@BartoszKlonowski, I understand your concern. However, since the issue has been idle for over a week without updates, I decided to step in to keep progress moving. Let me know and I can abandon this PR.

@BartoszKlonowski
Copy link
Contributor

@shethaadit No need to abandon, since you already spent some time on it.
Thing is, like I wrote: please ask person who volunteered if one still want to submit a PR. The bad luck today was that I was actually couple of minutes before submitting a PR as well, hence why saw your PR so quickly.

Thank you for the suggestion. Updating it.

Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com>
Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com>
Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com>
Copy link
Contributor

@BartoszKlonowski BartoszKlonowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me 👍
Sorry the fuss at the beginning, @shethaadit and thank you for handling this!

Let's hear from @BillWagner if there's something to improve.

@shethaadit
Copy link
Contributor Author

It looks good to me 👍 Sorry the fuss at the beginning, @shethaadit and thank you for handling this!

Let's hear from @BillWagner if there's something to improve.

Thank you @BartoszKlonowski for your suggestions. Appreciate your help and support. :)

Hi @BillWagner, please let me know if this PR looks okay.

Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. I had one grammar note, and then I'll :shipit:

docs/csharp/misc/cs0077.md Outdated Show resolved Hide resolved
@BillWagner BillWagner enabled auto-merge (squash) October 18, 2024 13:31
@BillWagner BillWagner merged commit 455d17b into dotnet:main Oct 18, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. csharp-errors-warnings/subsvc dotnet-csharp/svc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning page should be updated for pattern matching
3 participants