Docs: Documentation on the switch expression is missing

Created on 2 Oct 2019  Â·  12Comments  Â·  Source: dotnet/docs

This page is about the switch statement. Is it going to be expanded to include documentation about switch expression (https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#more-patterns-in-more-places) or is it going to be a separate page?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide Technology - C# Language Reference P1 doc-enhancement

All 12 comments

I would gather it would be a different page under that same parent. Considering they are different selection mechanisms and it discusses Switch Expression vs Switch Statement.

@BillWagner has a project tracking C# 8.0 updates. I'll add this issue to that project and let @BillWagner take it on from here.

Thanks!

GitHub is acting weird. I can't set backlog as the milestone..

I set the milestone to November. I want to update all the reference pages between now and .NET Core 3.1.

It is end of December and no documentation on switch expression! Come on guys, you could better.

@KUTlime currently the switch expression is briefly explained at the "What's new in C# 8.0" page:
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions

What, if any, do you think is missing there?

@pkulikov You just said it - briefly.

What is missing:

  • example with use of ternary operator
  • example with use of when condition
  • example with pattern matching
  • discussion when to use it and why
  • runtime benefits if any

I know how to do it. I'm trying to say that this could be helpful for newcomers to C# 8.0. The switch expressions are more or less equal to the switch statements. The switch expression deserves their own page with more examples and a structure as the switch statement page.

@KUTlime thank you!

I wonder what made you put the example with use of ternary operator first? Why particularly this operator?

Officially, I chose from-the-most-simple-use scenario ordering.

Unofficially, last two hours, I was analysing a weir problem in VS when switch expression and ternary operator was used. This brought me to this page. The problem was caused by R# and solved by update to the R# latest version but for a brief moment, I was convinced that ternary operator can't be used together with switch expression simply because of the fact that I wasn't able to find any proof that I'm wrong or vice versa.

By the way, the following C# 8.0 pattern matching tutorial contains examples with the switch expression:
https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/pattern-matching

That page can be a good way to learn about the switch expression, while there is no dedicated switch page.

In particular, it covers:

  • example with use of when condition
  • example with pattern matching

I'm starting to work on this, and it's exposing some flaws in the current organization of the language reference. I'd like feedback on the following ordered set of thoughts:

  1. The new information really doesn't belong on this page. The switch expression is different than the switch statement:

    • Most obviously, it is an expression, not a statement.

    • The syntax has many differences. => instead of : and break

    • The switch arms must be expressions (not statements, or blocks)

  2. This article is already rather long. I'm concerned with making it much longer by adding the information on switch expressions.
  3. The obvious location for a new article on switch expressions would be an Expressions section. But, there isn't one. All expressions are in the Operators section.

That leads me to this conclusion:

Is this the right time to rename the "Operators" section "Expressions", and add the new article there?

I propose "Yes".

If so, which of the sub-sections should be named expressions instead of operators?

I propose renaming these:

  • Member access expressions
  • ?: expression
  • await expression
  • default expression

Should there be others? Should some be dropped?

/cc @pkulikov for input.

@BillWagner I agree mostly with you and have had the similar thoughts about expressions.

Is this the right time to rename the "Operators" section "Expressions", and add the new article there?

Yes, however I suggest to rename the section to Operators and expressions (or Expressions and operators). What do you think?

As for renamings, I don't think ?: should be renamed, as it's widely known as "ternary operator", also "C# conditional operator" is more checked in Google than "C# conditional expression".

As for the member access, I think it should be "Member access operators and expressions", because invocation () is more an expression, but range .. is commonly referred as an operator.

With other two I agree and have some more, so:

  • await expression
  • default expression
  • stackalloc expression
  • nameof expression

What do you think?
As soon as we agree on renamings, I would like to submit the corresponding PR. There we can discuss the remaining minor details if any, and after it's merged, there are no blocks for the switch expression. Is that fine with you?

What do you think?

I like your additions @pkulikov

submit the corresponding PR. There we can discuss the remaining minor details if any, and after it's merged, there are no blocks for the switch expression. Is that fine with you?

Great plan. I agree. I'll start working on a draft, and then rebase once I've seen your PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stanuku picture stanuku  Â·  3Comments

sdmaclea picture sdmaclea  Â·  3Comments

JagathPrasad picture JagathPrasad  Â·  3Comments

skylerberg picture skylerberg  Â·  3Comments

svick picture svick  Â·  3Comments