Skip to content

Add explanation of why we need the when contextual keyword #40661

@Rabadash8820

Description

@Rabadash8820

Type of issue

Missing information

Description

I was wondering why it's really better to use C#'s contextual when keyword over just putting if/else inside a catch clause. This documentation really doesn't provide a reason, or even comparison of the example code to an example using if/then to show the syntactic sugar. I did find this article by Thomas Levesque explaining that when doesn't unwind the call stack. This information should be included in the official documentation.

Moreover, that article has given me the impression that almost every use of throw; (i.e., re-throwing an exception) should be avoided because it does unwind the call stack, and there is nothing about this in the official docs on when or try/catch. So maybe what I'm really asking for is a page about stack unwinding that is referenced from this when page. I did find this 2008 article about stack unwinding, but it's mostly talking about a debugger feature added in VB.NET 2005. Long story short, my understanding of exceptions and call stacks has been shaken, and I feel like I'm having to scour the internet for clarification that should be in the official docs.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/when

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/keywords/when.md

Document Version Independent Id

15502bbc-5355-2add-0fa8-ae31dc01c122

Article author

@BillWagner

Metadata

  • ID: 6ef10b29-fbf8-5af9-8f8b-a694a1416e33
  • Service: dotnet-csharp
  • Sub-service: lang-reference

Associated WorkItem - 479364

Metadata

Metadata

Labels

📌 seQUESTeredIdentifies that an issue has been imported into Quest.dotnet-csharp/svclang-reference/subsvcokr-qualityContent-quality KR: Concerns article defects (bugs), freshness, or build warnings.

Type

No type

Projects

Status

👀 In review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions