Skip to content

Commit f48987f

Browse files
pkulikovBillWagner
andauthored
C# reference: Consolidated articles about checked and unchecked statements (#30738)
* C# reference: Consolidated articles about checked and unchecked statements * Additional edits * Addressed feedback * Addressed feedback * Update docs/csharp/language-reference/statements/checked-and-unchecked.md Co-authored-by: Bill Wagner <[email protected]>
1 parent 19f23ad commit f48987f

File tree

24 files changed

+195
-382
lines changed

24 files changed

+195
-382
lines changed

.openpublishing.redirection.csharp.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,14 @@
455455
"source_path_from_root": "/docs/csharp/language-reference/keywords/char.md",
456456
"redirect_url": "/dotnet/csharp/language-reference/builtin-types/char"
457457
},
458+
{
459+
"source_path_from_root": "/docs/csharp/language-reference/keywords/checked.md",
460+
"redirect_url": "/dotnet/csharp/language-reference/statements/checked-and-unchecked"
461+
},
462+
{
463+
"source_path_from_root": "/docs/csharp/language-reference/keywords/checked-and-unchecked.md",
464+
"redirect_url": "/dotnet/csharp/language-reference/statements/checked-and-unchecked"
465+
},
458466
{
459467
"source_path_from_root": "/docs/csharp/language-reference/keywords/continue.md",
460468
"redirect_url": "/dotnet/csharp/language-reference/statements/jump-statements"
@@ -699,6 +707,10 @@
699707
"source_path_from_root": "/docs/csharp/language-reference/keywords/ulong.md",
700708
"redirect_url": "/dotnet/csharp/language-reference/builtin-types/integral-numeric-types"
701709
},
710+
{
711+
"source_path_from_root": "/docs/csharp/language-reference/keywords/unchecked.md",
712+
"redirect_url": "/dotnet/csharp/language-reference/statements/checked-and-unchecked"
713+
},
702714
{
703715
"source_path_from_root": "/docs/csharp/language-reference/keywords/ushort.md",
704716
"redirect_url": "/dotnet/csharp/language-reference/builtin-types/integral-numeric-types"

docs/csharp/language-reference/builtin-types/numeric-conversions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ The following table shows the predefined explicit conversions between the built-
7777
7878
Also note that:
7979

80-
- When you convert a value of an integral type to another integral type, the result depends on the overflow [checking context](../keywords/checked-and-unchecked.md). In a checked context, the conversion succeeds if the source value is within the range of the destination type. Otherwise, an <xref:System.OverflowException> is thrown. In an unchecked context, the conversion always succeeds, and proceeds as follows:
80+
- When you convert a value of an integral type to another integral type, the result depends on the [overflow-checking context](../statements/checked-and-unchecked.md). In a checked context, the conversion succeeds if the source value is within the range of the destination type. Otherwise, an <xref:System.OverflowException> is thrown. In an unchecked context, the conversion always succeeds, and proceeds as follows:
8181

8282
- If the source type is larger than the destination type, then the source value is truncated by discarding its "extra" most significant bits. The result is then treated as a value of the destination type.
8383

@@ -87,7 +87,7 @@ Also note that:
8787

8888
- When you convert a `decimal` value to an integral type, this value is rounded towards zero to the nearest integral value. If the resulting integral value is outside the range of the destination type, an <xref:System.OverflowException> is thrown.
8989

90-
- When you convert a `double` or `float` value to an integral type, this value is rounded towards zero to the nearest integral value. If the resulting integral value is outside the range of the destination type, the result depends on the overflow [checking context](../keywords/checked-and-unchecked.md). In a checked context, an <xref:System.OverflowException> is thrown, while in an unchecked context, the result is an unspecified value of the destination type.
90+
- When you convert a `double` or `float` value to an integral type, this value is rounded towards zero to the nearest integral value. If the resulting integral value is outside the range of the destination type, the result depends on the [overflow-checking context](../statements/checked-and-unchecked.md). In a checked context, an <xref:System.OverflowException> is thrown, while in an unchecked context, the result is an unspecified value of the destination type.
9191

9292
- When you convert `double` to `float`, the `double` value is rounded to the nearest `float` value. If the `double` value is too small or too large to fit into the `float` type, the result is zero or infinity.
9393

docs/csharp/language-reference/compiler-options/language.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ The following options control how the compiler interprets language features. The
2323

2424
## CheckForOverflowUnderflow
2525

26-
The **CheckForOverflowUnderflow** option specifies whether an integer arithmetic statement that results in a value that is outside the range of the data type causes a run-time exception.
26+
The **CheckForOverflowUnderflow** option controls the default overflow-checking context that defines the program behavior in the case of integer arithmetic overflow.
2727

2828
```xml
2929
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
3030
```
3131

32-
An integer arithmetic statement that is in the scope of a `checked` or `unchecked` keyword isn't subject to the effect of the **CheckForOverflowUnderflow** option. If an integer arithmetic statement that isn't in the scope of a `checked` or `unchecked` keyword results in a value outside the range of the data type, and **CheckForOverflowUnderflow** is `true`, that statement causes an exception at run time. If **CheckForOverflowUnderflow** is `false`, that statement doesn't cause an exception at run time. The default value for this option is `false`; overflow checking is disabled.
32+
When **CheckForOverflowUnderflow** is `true`, the default context is a checked context and overflow checking is enabled; otherwise, the default context is an unchecked context. The default value for this option is `false`, that is, overflow checking is disabled.
33+
34+
You can also explicitly control the overflow-checking context for the parts of your code by using the `checked` and `unchecked` statements.
35+
36+
For information about how the overflow-checking context affects operations and what operations are affected, see the [article about `checked` and `unchecked` statements](../statements/checked-and-unchecked.md).
3337

3438
## AllowUnsafeBlocks
3539

docs/csharp/language-reference/keywords/checked-and-unchecked.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

docs/csharp/language-reference/keywords/checked.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

docs/csharp/language-reference/keywords/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The first table in this topic lists keywords that are reserved identifiers in an
2828
[`case`](../statements/selection-statements.md#the-switch-statement)
2929
[`catch`](try-catch.md)
3030
[`char`](../builtin-types/char.md)
31-
[`checked`](checked.md)
31+
[`checked`](../statements/checked-and-unchecked.md)
3232
[`class`](class.md)
3333
[`const`](const.md)
3434
[`continue`](../statements/jump-statements.md#the-continue-statement)
@@ -94,7 +94,7 @@ The first table in this topic lists keywords that are reserved identifiers in an
9494
[`typeof`](../operators/type-testing-and-cast.md#typeof-operator)
9595
[`uint`](../builtin-types/integral-numeric-types.md)
9696
[`ulong`](../builtin-types/integral-numeric-types.md)
97-
[`unchecked`](unchecked.md)
97+
[`unchecked`](../statements/checked-and-unchecked.md)
9898
[`unsafe`](unsafe.md)
9999
[`ushort`](../builtin-types/integral-numeric-types.md)
100100
[`using`](using.md)

docs/csharp/language-reference/keywords/snippets/CheckedUnchecked.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

docs/csharp/language-reference/keywords/snippets/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ static async Task Main(string[] args)
1717
UsingStatements.Examples();
1818
Console.WriteLine("================= try-catch Keyword Examples ======================");
1919
await AsyncExceptionExamples.Examples();
20-
Console.WriteLine("================= try-catch Keyword Examples ======================");
21-
CheckedAndUnchecked.Examples();
2220
}
2321
}
2422
}

docs/csharp/language-reference/keywords/statement-keywords.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Statements are program instructions. Except as described in the topics reference
1717
|[Iteration statements](../statements/iteration-statements.md)|`do`, `for`, `foreach`, `while`|
1818
|[Jump statements](../statements/jump-statements.md)|`break`, `continue`, `goto`, `return`|
1919
|Exception handling statements|[throw](throw.md), [try-catch](try-catch.md), [try-finally](try-finally.md), [try-catch-finally](try-catch-finally.md)|
20-
|[Checked and unchecked](checked-and-unchecked.md)|[checked](checked.md), [unchecked](unchecked.md)|
20+
|[checked and unchecked statements](../statements/checked-and-unchecked.md)|`checked`, `unchecked`|
2121
|[fixed statement](fixed-statement.md)|[fixed](fixed-statement.md)|
2222
|[lock statement](../statements/lock.md)|`lock`|
2323
|[yield statement](yield.md)|`yield`|

docs/csharp/language-reference/keywords/unchecked.md

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)