Skip to content

C# reference: Consolidate the selection statements articles #25520

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

Merged
merged 6 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,10 @@
"source_path": "docs/csharp/language-reference/keywords/global.md",
"redirect_url": "/dotnet/csharp/language-reference/operators/namespace-alias-qualifier"
},
{
"source_path": "docs/csharp/language-reference/keywords/if-else.md",
"redirect_url": "/dotnet/csharp/language-reference/statements/selection-statements"
},
{
"source_path": "docs/csharp/language-reference/keywords/implicit-numeric-conversions-table.md",
"redirect_url": "/dotnet/csharp/language-reference/builtin-types/numeric-conversions"
Expand Down Expand Up @@ -1521,7 +1525,7 @@
},
{
"source_path": "docs/csharp/language-reference/keywords/selection-statements.md",
"redirect_url": "/dotnet/csharp/language-reference/keywords/statement-keywords"
"redirect_url": "/dotnet/csharp/language-reference/statements/selection-statements"
},
{
"source_path": "docs/csharp/language-reference/keywords/short.md",
Expand All @@ -1543,6 +1547,10 @@
"source_path": "docs/csharp/language-reference/keywords/struct.md",
"redirect_url": "/dotnet/csharp/language-reference/builtin-types/struct"
},
{
"source_path": "docs/csharp/language-reference/keywords/switch.md",
"redirect_url": "/dotnet/csharp/language-reference/statements/selection-statements"
},
{
"source_path": "docs/csharp/language-reference/keywords/true-false-operators.md",
"redirect_url": "/dotnet/csharp/language-reference/operators/true-false-operators"
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/fundamentals/functional/discards.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ Without assigning the task to a discard, the following code generates a compiler

- [Deconstructing tuples and other types](deconstruct.md)
- [`is` operator](../../language-reference/operators/is.md)
- [`switch` keyword](../../language-reference/keywords/switch.md)
- [`switch` expression](../../language-reference/operators/switch-expression.md)
2 changes: 1 addition & 1 deletion docs/csharp/fundamentals/tutorials/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,5 @@ You can continue with the [object oriented programming](oop.md) tutorial.

You can learn more about these concepts in these articles:

- [If and else statement](../../language-reference/keywords/if-else.md)
- [Selection statements](../../language-reference/statements/selection-statements.md)
- [Iteration statements](../../language-reference/statements/iteration-statements.md)
2 changes: 1 addition & 1 deletion docs/csharp/fundamentals/tutorials/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace toll_calculator
}
```

The preceding code uses a [`switch` expression](../../language-reference/operators/switch-expression.md) (not the same as a [`switch`](../../language-reference/keywords/switch.md) statement) that tests the [declaration pattern](../../language-reference/operators/patterns.md#declaration-and-type-patterns). A **switch expression** begins with the variable, `vehicle` in the preceding code, followed by the `switch` keyword. Next comes all the **switch arms** inside curly braces. The `switch` expression makes other refinements to the syntax that surrounds the `switch` statement. The `case` keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The `{ }` case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. The `{ }` case must follow the cases for each vehicle type. If the order were reversed, the `{ }` case would take precedence. Finally, the `null` [constant pattern](../../language-reference/operators/patterns.md#constant-pattern) detects when `null` is passed to this method. The `null` pattern can be last because the other patterns match only a non-null object of the correct type.
The preceding code uses a [`switch` expression](../../language-reference/operators/switch-expression.md) (not the same as a [`switch` statement](../../language-reference/statements/selection-statements.md#the-switch-statement)) that tests the [declaration pattern](../../language-reference/operators/patterns.md#declaration-and-type-patterns). A **switch expression** begins with the variable, `vehicle` in the preceding code, followed by the `switch` keyword. Next comes all the **switch arms** inside curly braces. The `switch` expression makes other refinements to the syntax that surrounds the `switch` statement. The `case` keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The `{ }` case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. The `{ }` case must follow the cases for each vehicle type. If the order were reversed, the `{ }` case would take precedence. Finally, the `null` [constant pattern](../../language-reference/operators/patterns.md#constant-pattern) detects when `null` is passed to this method. The `null` pattern can be last because the other patterns match only a non-null object of the correct type.

You can test this code using the following code in `Program.cs`:

Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/language-reference/builtin-types/bool.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ms.assetid: 551cfe35-2632-4343-af49-33ad12da08e2

The `bool` type keyword is an alias for the .NET <xref:System.Boolean?displayProperty=nameWithType> structure type that represents a Boolean value, which can be either `true` or `false`.

To perform logical operations with values of the `bool` type, use [Boolean logical](../operators/boolean-logical-operators.md) operators. The `bool` type is the result type of [comparison](../operators/comparison-operators.md) and [equality](../operators/equality-operators.md) operators. A `bool` expression can be a controlling conditional expression in the [if](../keywords/if-else.md), [do](../statements/iteration-statements.md#the-do-statement), [while](../statements/iteration-statements.md#the-while-statement), and [for](../statements/iteration-statements.md#the-for-statement) statements and in the [conditional operator `?:`](../operators/conditional-operator.md).
To perform logical operations with values of the `bool` type, use [Boolean logical](../operators/boolean-logical-operators.md) operators. The `bool` type is the result type of [comparison](../operators/comparison-operators.md) and [equality](../operators/equality-operators.md) operators. A `bool` expression can be a controlling conditional expression in the [if](../statements/selection-statements.md#the-if-statement), [do](../statements/iteration-statements.md#the-do-statement), [while](../statements/iteration-statements.md#the-while-statement), and [for](../statements/iteration-statements.md#the-for-statement) statements and in the [conditional operator `?:`](../operators/conditional-operator.md).

The default value of the `bool` type is `false`.

Expand Down
3 changes: 2 additions & 1 deletion docs/csharp/language-reference/builtin-types/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ For more information, see the following sections of the [C# language specificati
- [Enumeration format strings](../../../standard/base-types/enumeration-format-strings.md)
- [Design guidelines - Enum design](../../../standard/design-guidelines/enum.md)
- [Design guidelines - Enum naming conventions](../../../standard/design-guidelines/names-of-classes-structs-and-interfaces.md#naming-enumerations)
- [switch statement](../keywords/switch.md)
- [`switch` expression](../operators/switch-expression.md)
- [`switch` statement](../statements/selection-statements.md#the-switch-statement)
22 changes: 0 additions & 22 deletions docs/csharp/language-reference/compiler-messages/cs0151.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,3 @@ public class MyClass
}
}
```

## Example of void method
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This example doesn't fit the article anymore as it generates CS8119 error


A [void](../builtin-types/void.md) method invocation in a [switch](../keywords/switch.md) match expression generates CS0151. You can fix the error by calling a method that returns an integral type such as [int](../builtin-types/integral-numeric-types.md) or [long](../builtin-types/integral-numeric-types.md) instead.

```csharp
class C
{
static void Main()
{
switch (M()) // CS0151
{
default:
break;
}
}

static void M()
{
}
}
```
6 changes: 2 additions & 4 deletions docs/csharp/language-reference/compiler-messages/cs0163.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ms.assetid: 00139dcf-33cd-45ea-bf80-d6f26b10a5d2

Control cannot fall through from one case label ('label') to another

When a [switch statement](../keywords/switch.md) contains more than one switch section, you must explicitly terminate each section, including the last one, by using one of the following keywords:
When a [switch statement](../statements/selection-statements.md#the-switch-statement) contains more than one switch section, you must explicitly terminate each section, including the last one, by using one of the following keywords:

- [return](../keywords/return.md)

Expand All @@ -22,9 +22,7 @@ Control cannot fall through from one case label ('label') to another

- [throw](../keywords/throw.md)

- [continue](../keywords/continue.md)

If you want to implement "fall through" behavior from one section to the next, use `goto case #`. For more information and examples, see [switch](../keywords/switch.md).
If you want to implement "fall through" behavior from one section to the next, use `goto case #`.

The following sample generates CS0163.

Expand Down
8 changes: 4 additions & 4 deletions docs/csharp/language-reference/keywords/break.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ms.assetid: be2571ed-efb0-4965-b122-81e5b09db0b9
---
# break (C# Reference)

The `break` statement terminates the closest enclosing loop or [switch](./switch.md) statement in which it appears. Control is passed to the statement that follows the terminated statement, if any.
The `break` statement terminates the closest enclosing loop or [`switch` statement](../statements/selection-statements.md#the-switch-statement) in which it appears. Control is passed to the statement that follows the terminated statement, if any.

## Example 1

Expand All @@ -21,7 +21,7 @@ In this example, the conditional statement contains a counter that is supposed t

## Example 2

This example demonstrates the use of `break` in a [switch](./switch.md) statement.
This example demonstrates the use of `break` in a `switch` statement.

[!code-csharp[csrefKeywordsJump#2](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsJump/CS/csrefKeywordsJump.cs#2)]

Expand All @@ -40,7 +40,7 @@ In this example, the `break` statement is used to break out of an inner nested l

## Example 4

In this example, the `break` statement is only used to break out of the current branch during each iteration of the loop. The loop itself is unaffected by the instances of `break` that belong to the nested [switch](./switch.md) statement.
In this example, the `break` statement is only used to break out of the current branch during each iteration of the loop. The loop itself is unaffected by the instances of `break` that belong to the nested `switch` statement.

[!code-csharp[csrefKeywordsJump#8](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsJump/CS/csrefKeywordsJump.cs#8)]

Expand All @@ -53,4 +53,4 @@ In this example, the `break` statement is only used to break out of the current
- [C# Reference](../index.md)
- [C# Programming Guide](../../programming-guide/index.md)
- [C# Keywords](./index.md)
- [switch](./switch.md)
- [`switch` statement](../statements/selection-statements.md#the-switch-statement)
15 changes: 7 additions & 8 deletions docs/csharp/language-reference/keywords/default.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
description: "default - C# Reference"
title: "default - C# Reference"
description: "default - C# reference"
title: "default - C# reference"
ms.date: 04/28/2021
f1_keywords:
- "default"
Expand All @@ -9,16 +9,15 @@ helpviewer_keywords:
ms.assetid: 14c48aaa-7d35-4058-a1a4-f53353050579
---

# default (C# Reference)
# default (C# reference)

The `default` keyword can be used in three ways:
You can use the `default` keyword in the following contexts:

- To specify the default label in the [`switch` statement](switch.md).
- To specify the default case in the [`switch` statement](../statements/selection-statements.md#the-switch-statement).
- As the [default operator or literal](../operators/default.md) to produce the default value of a type.
- As the [`default` type constraint](where-generic-type-constraint.md) on a generic method override or explicit interface implementation.

## See also

- [C# Reference](../index.md)
- [C# Programming Guide](../../programming-guide/index.md)
- [C# Keywords](index.md)
- [C# reference](../index.md)
- [C# keywords](index.md)
2 changes: 1 addition & 1 deletion docs/csharp/language-reference/keywords/goto.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The `goto` statement is also useful to get out of deeply nested loops.

## Example 1

The following example demonstrates using `goto` in a [switch](switch.md) statement.
The following example demonstrates using `goto` in a [`switch` statement](../statements/selection-statements.md#the-switch-statement).

[!code-csharp[csrefKeywordsJump#4](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsJump/CS/csrefKeywordsJump.cs#4)]

Expand Down
139 changes: 0 additions & 139 deletions docs/csharp/language-reference/keywords/if-else.md

This file was deleted.

Loading