Skip to content

remove inner snippets + formatting #9925

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 1 commit into from
Jan 10, 2019
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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

24 changes: 13 additions & 11 deletions docs/csharp/language-reference/keywords/when.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "when - C# Reference"
title: "when contextual keyword - C# Reference"
ms.custom: seodec18

ms.date: 03/07/2017
Expand All @@ -10,7 +10,7 @@ helpviewer_keywords:
- "when keyword [C#]"
ms.assetid: dd543335-ae37-48ac-9560-bd5f047b9aea
---
# when (C# Reference)
# when (C# Reference)

You can use the `when` contextual keyword to specify a filter condition in two contexts:

Expand All @@ -24,27 +24,29 @@ Starting with C# 6, `when` can be used in a `catch` statement to specify a condi
```csharp
catch (ExceptionType [e]) when (expr)
```
where *expr* is an expression that evaluates to a Boolean value. If it returns `true`, the exception handler executes; if `false`, it does not.

where *expr* is an expression that evaluates to a Boolean value. If it returns `true`, the exception handler executes; if `false`, it does not.

The following example uses the `when` keyword to conditionally execute handlers for an <xref:System.Net.Http.HttpRequestException> depending on the text of the exception message.

[!code-csharp[when-with-catch](../../../../samples/snippets/csharp/language-reference/keywords/when/catch.cs)]
[!code-csharp[when-with-catch](~/samples/snippets/csharp/language-reference/keywords/when/catch.cs)]

## `when` in a `switch` statement

Starting with C# 7.0, `case` labels no longer need be mutually exclusive, and the order in which `case` labels appear in a `switch` statement can determine which switch block executes. The `when` keyword can be used to specify a filter condition that causes its associated case label to be true only if the filter condition is also true. Its syntax is:

```csharp
case (expr) when (when-condition):
```
where *expr* is a constant pattern or type pattern that is compared to the match expression, and *when-condition* is any Boolean expression.

The following example uses the `when` keyword to test for `Shape` objects that have an area of zero, as well as to test for a variety of `Shape` objects that have an area greater than zero.
where *expr* is a constant pattern or type pattern that is compared to the match expression, and *when-condition* is any Boolean expression.

The following example uses the `when` keyword to test for `Shape` objects that have an area of zero, as well as to test for a variety of `Shape` objects that have an area greater than zero.

[!code-csharp[when-with-case#1](../../../../samples/snippets/csharp/language-reference/keywords/when/when.cs#1)]
[!code-csharp[when-with-case#1](~/samples/snippets/csharp/language-reference/keywords/when/when.cs#1)]

## See also

- [switch statement](switch.md)
- [try/catch statement](try-catch.md)
- [try/catch/finally statement](try-catch-finally.md)
- [switch statement](switch.md)
- [try/catch statement](try-catch.md)
- [try/catch/finally statement](try-catch-finally.md)
69 changes: 37 additions & 32 deletions docs/csharp/language-reference/keywords/where-clause.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,40 @@ helpviewer_keywords:
ms.assetid: 7f9bf952-7744-4f91-b676-cddb55d107c3
---
# where clause (C# Reference)
The `where` clause is used in a query expression to specify which elements from the data source will be returned in the query expression. It applies a Boolean condition (*predicate*) to each source element (referenced by the range variable) and returns those for which the specified condition is true. A single query expression may contain multiple `where` clauses and a single clause may contain multiple predicate subexpressions.

## Example
In the following example, the `where` clause filters out all numbers except those that are less than five. If you remove the `where` clause, all numbers from the data source would be returned. The expression `num < 5` is the predicate that is applied to each element.

[!code-csharp[cscsrefQueryKeywords#5](../../../csharp/language-reference/keywords/codesnippet/CSharp/where-clause_1.cs)]

## Example
Within a single `where` clause, you can specify as many predicates as necessary by using the [&&](../../../csharp/language-reference/operators/conditional-and-operator.md) and [&#124;&#124;](../../../csharp/language-reference/operators/conditional-or-operator.md) operators. In the following example, the query specifies two predicates in order to select only the even numbers that are less than five.

[!code-csharp[cscsrefQueryKeywords#6](../../../csharp/language-reference/keywords/codesnippet/CSharp/where-clause_2.cs)]

## Example
A `where` clause may contain one or more methods that return Boolean values. In the following example, the `where` clause uses a method to determine whether the current value of the range variable is even or odd.

[!code-csharp[cscsrefQueryKeywords#7](../../../csharp/language-reference/keywords/codesnippet/CSharp/where-clause_3.cs)]

## Remarks
The `where` clause is a filtering mechanism. It can be positioned almost anywhere in a query expression, except it cannot be the first or last clause. A `where` clause may appear either before or after a [group](../../../csharp/language-reference/keywords/group-clause.md) clause depending on whether you have to filter the source elements before or after they are grouped.

If a specified predicate is not valid for the elements in the data source, a compile-time error will result. This is one benefit of the strong type-checking provided by [!INCLUDE[vbteclinq](~/includes/vbteclinq-md.md)].

At compile time the `where` keyword is converted into a call to the <xref:System.Linq.Enumerable.Where%2A> Standard Query Operator method.

## See Also

- [Query Keywords (LINQ)](../../../csharp/language-reference/keywords/query-keywords.md)
- [from clause](../../../csharp/language-reference/keywords/from-clause.md)
- [select clause](../../../csharp/language-reference/keywords/select-clause.md)
- [Filtering Data](../../programming-guide/concepts/linq/filtering-data.md)
- [LINQ Query Expressions](../../../csharp/programming-guide/linq-query-expressions/index.md)
- [Getting Started with LINQ in C#](../../../csharp/programming-guide/concepts/linq/getting-started-with-linq.md)

The `where` clause is used in a query expression to specify which elements from the data source will be returned in the query expression. It applies a Boolean condition (*predicate*) to each source element (referenced by the range variable) and returns those for which the specified condition is true. A single query expression may contain multiple `where` clauses and a single clause may contain multiple predicate subexpressions.

## Example

In the following example, the `where` clause filters out all numbers except those that are less than five. If you remove the `where` clause, all numbers from the data source would be returned. The expression `num < 5` is the predicate that is applied to each element.

[!code-csharp[cscsrefQueryKeywords#5](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsCsrefQueryKeywords/CS/Where.cs#5)]

## Example

Within a single `where` clause, you can specify as many predicates as necessary by using the [&&](../operators/conditional-and-operator.md) and [&#124;&#124;](../operators/conditional-or-operator.md) operators. In the following example, the query specifies two predicates in order to select only the even numbers that are less than five.

[!code-csharp[cscsrefQueryKeywords#6](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsCsrefQueryKeywords/CS/Where.cs#6)]

## Example

A `where` clause may contain one or more methods that return Boolean values. In the following example, the `where` clause uses a method to determine whether the current value of the range variable is even or odd.

[!code-csharp[cscsrefQueryKeywords#7](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsCsrefQueryKeywords/CS/Where.cs#7)]

## Remarks

The `where` clause is a filtering mechanism. It can be positioned almost anywhere in a query expression, except it cannot be the first or last clause. A `where` clause may appear either before or after a [group](group-clause.md) clause depending on whether you have to filter the source elements before or after they are grouped.

If a specified predicate is not valid for the elements in the data source, a compile-time error will result. This is one benefit of the strong type-checking provided by [!INCLUDE[vbteclinq](~/includes/vbteclinq-md.md)].

At compile time the `where` keyword is converted into a call to the <xref:System.Linq.Enumerable.Where%2A> Standard Query Operator method.

## See also

- [Query Keywords (LINQ)](query-keywords.md)
- [from clause](from-clause.md)
- [select clause](select-clause.md)
- [Filtering Data](../../programming-guide/concepts/linq/filtering-data.md)
- [LINQ Query Expressions](../../../csharp/programming-guide/linq-query-expressions/index.md)
- [Getting Started with LINQ in C#](../../programming-guide/concepts/linq/getting-started-with-linq.md)
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ For details on the syntax and use of constraints, see [Constraints on Type Param

## See also

- [C# Reference](../../../csharp/language-reference/index.md)
- [C# Programming Guide](../../../csharp/programming-guide/index.md)
- [Introduction to Generics](../../../csharp/programming-guide/generics/introduction-to-generics.md)
- [new Constraint](../../../csharp/language-reference/keywords/new-constraint.md)
- [Constraints on Type Parameters](../../../csharp/programming-guide/generics/constraints-on-type-parameters.md)
- [C# Reference](../../../csharp/language-reference/index.md)
- [C# Programming Guide](../../../csharp/programming-guide/index.md)
- [Introduction to Generics](../../../csharp/programming-guide/generics/introduction-to-generics.md)
- [new Constraint](../../../csharp/language-reference/keywords/new-constraint.md)
- [Constraints on Type Parameters](../../../csharp/programming-guide/generics/constraints-on-type-parameters.md)
Loading