From 7ace4e71f2a526ac45c69a146f8792c51e1cf21b Mon Sep 17 00:00:00 2001 From: Maira Wenzel Date: Wed, 28 Nov 2018 01:50:37 -0800 Subject: [PATCH 1/4] remove inner snippets + formatting --- .../codesnippet/CSharp/using-directive_1.cs | 20 -- .../codesnippet/CSharp/using-directive_2.cs | 54 ------ .../codesnippet/CSharp/using-statement_1.cs | 4 - .../codesnippet/CSharp/using-statement_2.cs | 12 -- .../codesnippet/CSharp/using-statement_3.cs | 5 - .../codesnippet/CSharp/using-statement_4.cs | 9 - .../language-reference/keywords/ushort.md | 174 +++++++++--------- .../keywords/using-directive.md | 168 +++++++++-------- .../keywords/using-statement.md | 86 ++++----- .../keywords/using-static.md | 32 ++-- .../language-reference/keywords/using.md | 29 +-- 11 files changed, 251 insertions(+), 342 deletions(-) delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/using-directive_1.cs delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/using-directive_2.cs delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_1.cs delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_2.cs delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_3.cs delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_4.cs diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-directive_1.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-directive_1.cs deleted file mode 100644 index 0dfbcdb403272..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-directive_1.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace PC -{ - // Define an alias for the nested namespace. - using Project = PC.MyCompany.Project; - class A - { - void M() - { - // Use the alias - Project.MyClass mc = new Project.MyClass(); - } - } - namespace MyCompany - { - namespace Project - { - public class MyClass { } - } - } -} \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-directive_2.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-directive_2.cs deleted file mode 100644 index ef965181ab0ad..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-directive_2.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; - -// Using alias directive for a class. -using AliasToMyClass = NameSpace1.MyClass; - -// Using alias directive for a generic class. -using UsingAlias = NameSpace2.MyClass; - -namespace NameSpace1 -{ - public class MyClass - { - public override string ToString() - { - return "You are in NameSpace1.MyClass."; - } - } - -} - -namespace NameSpace2 -{ - class MyClass - { - public override string ToString() - { - return "You are in NameSpace2.MyClass."; - } - } -} - -namespace NameSpace3 -{ - // Using directive: - using NameSpace1; - // Using directive: - using NameSpace2; - - class MainClass - { - static void Main() - { - AliasToMyClass instance1 = new AliasToMyClass(); - Console.WriteLine(instance1); - - UsingAlias instance2 = new UsingAlias(); - Console.WriteLine(instance2); - - } - } -} -// Output: -// You are in NameSpace1.MyClass. -// You are in NameSpace2.MyClass. \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_1.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_1.cs deleted file mode 100644 index d6ec72b214f2c..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_1.cs +++ /dev/null @@ -1,4 +0,0 @@ - using (Font font1 = new Font("Arial", 10.0f)) - { - byte charset = font1.GdiCharSet; - } \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_2.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_2.cs deleted file mode 100644 index 22f517dbef334..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_2.cs +++ /dev/null @@ -1,12 +0,0 @@ - { - Font font1 = new Font("Arial", 10.0f); - try - { - byte charset = font1.GdiCharSet; - } - finally - { - if (font1 != null) - ((IDisposable)font1).Dispose(); - } - } \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_3.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_3.cs deleted file mode 100644 index a45ecee5519b1..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_3.cs +++ /dev/null @@ -1,5 +0,0 @@ - using (Font font3 = new Font("Arial", 10.0f), - font4 = new Font("Arial", 10.0f)) - { - // Use font3 and font4. - } \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_4.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_4.cs deleted file mode 100644 index ee4f1a21c80e8..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/using-statement_4.cs +++ /dev/null @@ -1,9 +0,0 @@ - Font font2 = new Font("Arial", 10.0f); - using (font2) // not recommended - { - // use font2 - } - // font2 is still in scope - // but the method call throws an exception - float f = font2.GetHeight(); - diff --git a/docs/csharp/language-reference/keywords/ushort.md b/docs/csharp/language-reference/keywords/ushort.md index a7471aafc9fa7..1e84ce0db5dc3 100644 --- a/docs/csharp/language-reference/keywords/ushort.md +++ b/docs/csharp/language-reference/keywords/ushort.md @@ -1,110 +1,112 @@ --- -title: "ushort (C# Reference)" +title: "ushort keyword (C# Reference)" ms.date: 03/14/2017 -f1_keywords: +f1_keywords: - "ushort" - "ushort_CSharpKeyword" -helpviewer_keywords: +helpviewer_keywords: - "ushort keyword [C#]" ms.assetid: 1a7dbaae-b7a0-4111-872a-c88a6d3981ac --- # ushort (C# Reference) -The `ushort` keyword indicates an integral data type that stores values according to the size and range shown in the following table. - -|Type|Range|Size|.NET type| -|----------|-----------|----------|-------------------------| -|`ushort`|0 to 65,535|Unsigned 16-bit integer|| - -## Literals +The `ushort` keyword indicates an integral data type that stores values according to the size and range shown in the following table. + +|Type|Range|Size|.NET type| +|----------|-----------|----------|-------------------------| +|`ushort`|0 to 65,535|Unsigned 16-bit integer|| + +## Literals You can declare and initialize a `ushort` variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7.0) a binary literal to it. If the integer literal is outside the range of `ushort` (that is, if it is less than or greater than ), a compilation error occurs. -In the following example, integers equal to 65,034 that are represented as decimal, hexadecimal, and binary literals are implicitly converted from [int](../../../csharp/language-reference/keywords/int.md) to `ushort` values. - -[!code-csharp[UShort](../../../../samples/snippets/csharp/language-reference/keywords/numeric-literals.cs#UShort)] +In the following example, integers equal to 65,034 that are represented as decimal, hexadecimal, and binary literals are implicitly converted from [int](int.md) to `ushort` values. -> [!NOTE] +[!code-csharp[UShort](~/samples/snippets/csharp/language-reference/keywords/numeric-literals.cs#UShort)] + +> [!NOTE] > You use the prefix `0x` or `0X` to denote a hexadecimal literal and the prefix `0b` or `0B` to denote a binary literal. Decimal literals have no prefix. -Starting with C# 7.0, a couple of features have been added to enhance readability. - - C# 7.0 allows the usage of the underscore character, `_`, as a digit separator. - - C# 7.2 allows `_` to be used as a digit separator for a binary or hexadecimal literal, after the prefix. A decimal literal isn't permitted to have a leading underscore. +Starting with C# 7.0, a couple of features have been added to enhance readability: + +- C# 7.0 allows the usage of the underscore character, `_`, as a digit separator. +- C# 7.2 allows `_` to be used as a digit separator for a binary or hexadecimal literal, after the prefix. A decimal literal isn't permitted to have a leading underscore. Some examples are shown below. -[!code-csharp[UShort](../../../../samples/snippets/csharp/language-reference/keywords/numeric-literals.cs#UShortS)] - +[!code-csharp[UShort](~/samples/snippets/csharp/language-reference/keywords/numeric-literals.cs#UShortS)] + ## Compiler overload resolution - - A cast must be used when you call overloaded methods. Consider, for example, the following overloaded methods that use `ushort` and [int](../../../csharp/language-reference/keywords/int.md) parameters: - -```csharp -public static void SampleMethod(int i) {} -public static void SampleMethod(ushort s) {} -``` - - Using the `ushort` cast guarantees that the correct type is called, for example: - -```csharp -// Calls the method with the int parameter: -SampleMethod(5); -// Calls the method with the ushort parameter: -SampleMethod((ushort)5); -``` - -## Conversions - There is a predefined implicit conversion from `ushort` to [int](../../../csharp/language-reference/keywords/int.md), [uint](../../../csharp/language-reference/keywords/uint.md), [long](../../../csharp/language-reference/keywords/long.md), [ulong](../../../csharp/language-reference/keywords/ulong.md), [float](../../../csharp/language-reference/keywords/float.md), [double](../../../csharp/language-reference/keywords/double.md), or [decimal](../../../csharp/language-reference/keywords/decimal.md). - - There is a predefined implicit conversion from [byte](../../../csharp/language-reference/keywords/byte.md) or [char](../../../csharp/language-reference/keywords/char.md) to `ushort`. Otherwise a cast must be used to perform an explicit conversion. Consider, for example, the following two `ushort` variables `x` and `y`: - -```csharp -ushort x = 5, y = 12; -``` - - The following assignment statement will produce a compilation error, because the arithmetic expression on the right side of the assignment operator evaluates to `int` by default. - -```csharp -ushort z = x + y; // Error: conversion from int to ushort -``` - - To fix this problem, use a cast: - -```csharp -ushort z = (ushort)(x + y); // OK: explicit conversion -``` - - It is possible though to use the following statements, where the destination variable has the same storage size or a larger storage size: - + +A cast must be used when you call overloaded methods. Consider, for example, the following overloaded methods that use `ushort` and [int](int.md) parameters: + +```csharp +public static void SampleMethod(int i) {} +public static void SampleMethod(ushort s) {} +``` + +Using the `ushort` cast guarantees that the correct type is called, for example: + +```csharp +// Calls the method with the int parameter: +SampleMethod(5); +// Calls the method with the ushort parameter: +SampleMethod((ushort)5); +``` + +## Conversions + +There is a predefined implicit conversion from `ushort` to [int](int.md), [uint](uint.md), [long](long.md), [ulong](ulong.md), [float](float.md), [double](double.md), or [decimal](decimal.md). + +There is a predefined implicit conversion from [byte](byte.md) or [char](char.md) to `ushort`. Otherwise a cast must be used to perform an explicit conversion. Consider, for example, the following two `ushort` variables `x` and `y`: + ```csharp -int m = x + y; -long n = x + y; -``` - - Notice also that there is no implicit conversion from floating-point types to `ushort`. For example, the following statement generates a compiler error unless an explicit cast is used: - -```csharp -// Error -- no implicit conversion from double: -ushort x = 3.0; -// OK -- explicit conversion: -ushort y = (ushort)3.0; -``` - - For information about arithmetic expressions with mixed floating-point types and integral types, see [float](../../../csharp/language-reference/keywords/float.md) and [double](../../../csharp/language-reference/keywords/double.md). - - For more information about implicit numeric conversion rules, see the [Implicit Numeric Conversions Table](../../../csharp/language-reference/keywords/implicit-numeric-conversions-table.md). - -## C# Language Specification +ushort x = 5, y = 12; +``` + +The following assignment statement will produce a compilation error, because the arithmetic expression on the right side of the assignment operator evaluates to `int` by default. + +```csharp +ushort z = x + y; // Error: conversion from int to ushort +``` + +To fix this problem, use a cast: + +```csharp +ushort z = (ushort)(x + y); // OK: explicit conversion +``` + +It is possible though to use the following statements, where the destination variable has the same storage size or a larger storage size: + +```csharp +int m = x + y; +long n = x + y; +``` + +Notice also that there is no implicit conversion from floating-point types to `ushort`. For example, the following statement generates a compiler error unless an explicit cast is used: + +```csharp +// Error -- no implicit conversion from double: +ushort x = 3.0; +// OK -- explicit conversion: +ushort y = (ushort)3.0; +``` + +For information about arithmetic expressions with mixed floating-point types and integral types, see [float](float.md) and [double](double.md). + +For more information about implicit numeric conversion rules, see the [Implicit Numeric Conversions Table](implicit-numeric-conversions-table.md). + +## C# Language Specification For more information, see [Integral types](~/_csharplang/spec/types.md#integral-types) in the [C# Language Specification](../language-specification/index.md). The language specification is the definitive source for C# syntax and usage. - + ## See Also -- -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Programming Guide](../../../csharp/programming-guide/index.md) -- [C# Keywords](../../../csharp/language-reference/keywords/index.md) -- [Integral Types Table](../../../csharp/language-reference/keywords/integral-types-table.md) -- [Built-In Types Table](../../../csharp/language-reference/keywords/built-in-types-table.md) -- [Implicit Numeric Conversions Table](../../../csharp/language-reference/keywords/implicit-numeric-conversions-table.md) -- [Explicit Numeric Conversions Table](../../../csharp/language-reference/keywords/explicit-numeric-conversions-table.md) +- +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [C# Keywords](index.md) +- [Integral Types Table](integral-types-table.md) +- [Built-In Types Table](built-in-types-table.md) +- [Implicit Numeric Conversions Table](implicit-numeric-conversions-table.md) +- [Explicit Numeric Conversions Table](explicit-numeric-conversions-table.md) diff --git a/docs/csharp/language-reference/keywords/using-directive.md b/docs/csharp/language-reference/keywords/using-directive.md index 6076eeeccd0cb..8c2c735ccefcf 100644 --- a/docs/csharp/language-reference/keywords/using-directive.md +++ b/docs/csharp/language-reference/keywords/using-directive.md @@ -1,94 +1,100 @@ --- title: "using Directive (C# Reference)" ms.date: 07/20/2015 -helpviewer_keywords: +helpviewer_keywords: - "using directive [C#]" ms.assetid: b42b8e61-5e7e-439c-bb71-370094b44ae8 --- # using Directive (C# Reference) -The `using` directive has three uses: - -- To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace: - - ```csharp - using System.Text; - ``` - -- To allow you to access static members and nested types of a type without having to qualify the access with the type name. - - ```csharp - using static System.Math; - ``` - + +The `using` directive has three uses: + +- To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace: + + ```csharp + using System.Text; + ``` + +- To allow you to access static members and nested types of a type without having to qualify the access with the type name. + + ```csharp + using static System.Math; + ``` + For more information, see the [using static directive](using-static.md). -- To create an alias for a namespace or a type. This is called a *using alias directive*. - - ```csharp - using Project = PC.MyCompany.Project; - ``` - - The `using` keyword is also used to create *using statements*, which help ensure that objects such as files and fonts are handled correctly. See [using Statement](../../../csharp/language-reference/keywords/using-statement.md) for more information. - -## Using Static Type - You can access static members of a type without having to qualify the access with the type name: - -```csharp -using static System.Console; -using static System.Math; -class Program -{ - static void Main() - { - WriteLine(Sqrt(3*3 + 4*4)); - } -} -``` - -## Remarks - The scope of a `using` directive is limited to the file in which it appears. - - The `using` directive can appear: +- To create an alias for a namespace or a type. This is called a *using alias directive*. + + ```csharp + using Project = PC.MyCompany.Project; + ``` + +The `using` keyword is also used to create *using statements*, which help ensure that objects such as files and fonts are handled correctly. See [using Statement](using-statement.md) for more information. + +## Using static type + +You can access static members of a type without having to qualify the access with the type name: + +```csharp +using static System.Console; +using static System.Math; +class Program +{ + static void Main() + { + WriteLine(Sqrt(3*3 + 4*4)); + } +} +``` + +## Remarks + +The scope of a `using` directive is limited to the file in which it appears. + +The `using` directive can appear: + - At the beginning of a source code file, before any namespace or type definitions. - In any namespace, but before any namespace or types declared in this namespace. Otherwise, compiler error [CS1529](../../misc/cs1529.md) is generated. - - Create a `using` alias directive to make it easier to qualify an identifier to a namespace or type. In any `using` directive, the fully-qualified namespace or type must be used regardless of the `using` directives that come before it. No `using` alias can be used in the declaration of a `using` directive. For example, the following generates a compiler error: - ```csharp - using s = System.Text; - using s.RegularExpressions; - ``` - - Create a `using` directive to use the types in a namespace without having to specify the namespace. A `using` directive does not give you access to any namespaces that are nested in the namespace you specify. - - Namespaces come in two categories: user-defined and system-defined. User-defined namespaces are namespaces defined in your code. For a list of the system-defined namespaces, see [.NET API Browser](https://docs.microsoft.com/dotnet/api/). - - For examples on referencing methods in other assemblies, see [Create and Use Assemblies Using the Command Line](../../programming-guide/concepts/assemblies-gac/how-to-create-and-use-assemblies-using-the-command-line.md). - -## Example 1 - - The following example shows how to define and use a `using` alias for a namespace: - - [!code-csharp[csrefKeywordsNamespace#8](../../../csharp/language-reference/keywords/codesnippet/CSharp/using-directive_1.cs)] - - A using alias directive cannot have an open generic type on the right hand side. For example, you cannot create a using alias for a List\, but you can create one for a List\. - -## Example 2 - - The following example shows how to define a `using` directive and a `using` alias for a class: - - [!code-csharp[csrefKeywordsNamespace#9](../../../csharp/language-reference/keywords/codesnippet/CSharp/using-directive_2.cs)] - -## C# Language Specification - [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See Also - -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Programming Guide](../../../csharp/programming-guide/index.md) -- [Using Namespaces](../../../csharp/programming-guide/namespaces/using-namespaces.md) -- [C# Keywords](../../../csharp/language-reference/keywords/index.md) -- [Namespace Keywords](../../../csharp/language-reference/keywords/namespace-keywords.md) -- [Namespaces](../../../csharp/programming-guide/namespaces/index.md) -- [using Statement](../../../csharp/language-reference/keywords/using-statement.md) + +Create a `using` alias directive to make it easier to qualify an identifier to a namespace or type. In any `using` directive, the fully-qualified namespace or type must be used regardless of the `using` directives that come before it. No `using` alias can be used in the declaration of a `using` directive. For example, the following generates a compiler error: + +```csharp +using s = System.Text; +using s.RegularExpressions; +``` + +Create a `using` directive to use the types in a namespace without having to specify the namespace. A `using` directive does not give you access to any namespaces that are nested in the namespace you specify. + +Namespaces come in two categories: user-defined and system-defined. User-defined namespaces are namespaces defined in your code. For a list of the system-defined namespaces, see [.NET API Browser](https://docs.microsoft.com/dotnet/api/). + +For examples on referencing methods in other assemblies, see [Create and Use Assemblies Using the Command Line](../../programming-guide/concepts/assemblies-gac/how-to-create-and-use-assemblies-using-the-command-line.md). + +## Example 1 + +The following example shows how to define and use a `using` alias for a namespace: + +[!code-csharp[csrefKeywordsNamespace#8](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsNamespace/CS/csrefKeywordsNamespace2.cs#8)] + +A using alias directive cannot have an open generic type on the right hand side. For example, you cannot create a using alias for a List\, but you can create one for a `List`. + +## Example 2 + +The following example shows how to define a `using` directive and a `using` alias for a class: + +[!code-csharp[csrefKeywordsNamespace#9](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsNamespace/CS/csrefKeywordsNamespace2.cs#9)] + +## C# language specification + +For more information, see [Using directives](~/_csharplang/spec/namespaces.md#using-directives) in the [C# Language Specification](../language-specification/index.md). The language specification is the definitive source for C# syntax and usage. + +## See also + +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [Using Namespaces](../../programming-guide/namespaces/using-namespaces.md) +- [C# Keywords](index.md) +- [Namespace Keywords](namespace-keywords.md) +- [Namespaces](../../programming-guide/namespaces/index.md) +- [using Statement](using-statement.md) \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/using-statement.md b/docs/csharp/language-reference/keywords/using-statement.md index faacee7fc55f6..eb47317e16635 100644 --- a/docs/csharp/language-reference/keywords/using-statement.md +++ b/docs/csharp/language-reference/keywords/using-statement.md @@ -1,48 +1,52 @@ --- -title: "using Statement (C# Reference)" +title: "using statement (C# Reference)" ms.date: 07/20/2015 -helpviewer_keywords: +helpviewer_keywords: - "using statement [C#]" ms.assetid: afc355e6-f0b9-4240-94dd-0d93f17d9fc3 --- -# using Statement (C# Reference) -Provides a convenient syntax that ensures the correct use of objects. - -## Example - The following example shows how to use the `using` statement. - - [!code-csharp[csrefKeywordsNamespace#4](../../../csharp/language-reference/keywords/codesnippet/CSharp/using-statement_1.cs)] - -## Remarks - and are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the interface. - -When the lifetime of an `IDisposable` object is limited to a single method, you should declare and instantiate it in the `using` statement. The `using` statement calls the method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as is called. Within the `using` block, the object is read-only and cannot be modified or reassigned. - - The `using` statement ensures that is called even if an exception occurs within the `using` block. You can achieve the same result by putting the object inside a `try` block and then calling in a `finally` block; in fact, this is how the `using` statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object): - - [!code-csharp[csrefKeywordsNamespace#5](../../../csharp/language-reference/keywords/codesnippet/CSharp/using-statement_2.cs)] - - For more information about the `try`-`finally` statement, see the [try-finally](try-finally.md) topic. - - Multiple instances of a type can be declared in the `using` statement, as shown in the following example: - - [!code-csharp[csrefKeywordsNamespace#6](../../../csharp/language-reference/keywords/codesnippet/CSharp/using-statement_3.cs)] - - You can instantiate the resource object and then pass the variable to the `using` statement, but this is not a best practice. In this case, after control leaves the `using` block, the object remains in scope but probably has no access to its unmanaged resources. In other words, it's not fully initialized anymore. If you try to use the object outside the `using` block, you risk causing an exception to be thrown. For this reason, it's generally better to instantiate the object in the `using` statement and limit its scope to the `using` block. - - [!code-csharp[csrefKeywordsNamespace#7](../../../csharp/language-reference/keywords/codesnippet/CSharp/using-statement_4.cs)] - +# using statement (C# Reference) + +Provides a convenient syntax that ensures the correct use of objects. + +## Example + +The following example shows how to use the `using` statement. + +[!code-csharp[csrefKeywordsNamespace#4](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsNamespace/CS/csrefKeywordsNamespace.cs#4)] + +## Remarks + + and are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the interface. + +When the lifetime of an `IDisposable` object is limited to a single method, you should declare and instantiate it in the `using` statement. The `using` statement calls the method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as is called. Within the `using` block, the object is read-only and cannot be modified or reassigned. + +The `using` statement ensures that is called even if an exception occurs within the `using` block. You can achieve the same result by putting the object inside a `try` block and then calling in a `finally` block; in fact, this is how the `using` statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object): + +[!code-csharp[csrefKeywordsNamespace#5](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsNamespace/CS/csrefKeywordsNamespace.cs#5)] + +For more information about the `try`-`finally` statement, see the [try-finally](try-finally.md) topic. + +Multiple instances of a type can be declared in the `using` statement, as shown in the following example: + +[!code-csharp[csrefKeywordsNamespace#6](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsNamespace/CS/csrefKeywordsNamespace.cs#6)] + +You can instantiate the resource object and then pass the variable to the `using` statement, but this is not a best practice. In this case, after control leaves the `using` block, the object remains in scope but probably has no access to its unmanaged resources. In other words, it's not fully initialized anymore. If you try to use the object outside the `using` block, you risk causing an exception to be thrown. For this reason, it's generally better to instantiate the object in the `using` statement and limit its scope to the `using` block. + +[!code-csharp[csrefKeywordsNamespace#7](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsNamespace/CS/csrefKeywordsNamespace.cs#7)] + For more information about disposing of `IDisposable` objects, see [Using objects that implement IDisposable](../../../standard/garbage-collection/using-objects.md). -## C# Language Specification - [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See Also - -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Programming Guide](../../../csharp/programming-guide/index.md) -- [C# Keywords](../../../csharp/language-reference/keywords/index.md) -- [using Directive](../../../csharp/language-reference/keywords/using-directive.md) -- [Garbage Collection](../../../standard/garbage-collection/index.md) -- [Using objects that implement IDisposable](../../../standard/garbage-collection/using-objects.md) -- [IDisposable interface](xref:System.IDisposable) +## C# language specification + +For more information, see [The using statement](~/_csharplang/spec/statements.md#the-using-statement) in the [C# Language Specification](../language-specification/index.md). The language specification is the definitive source for C# syntax and usage. + +## See also + +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [C# Keywords](index.md) +- [using Directive](using-directive.md) +- [Garbage Collection](../../../standard/garbage-collection/index.md) +- [Using objects that implement IDisposable](../../../standard/garbage-collection/using-objects.md) +- [IDisposable interface](xref:System.IDisposable) \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/using-static.md b/docs/csharp/language-reference/keywords/using-static.md index 6717a625074ca..72500260fc33c 100644 --- a/docs/csharp/language-reference/keywords/using-static.md +++ b/docs/csharp/language-reference/keywords/using-static.md @@ -22,34 +22,34 @@ The `using static` directive applies to any type that has static members (or nes The `using static` directive was introduced in C# 6. ## Remarks - + Ordinarily, when you call a static member, you provide the type name along with the member name. Repeatedly entering the same type name to invoke members of the type can result in verbose, obscure code. For example, the following definition of a `Circle` class references a number of members of the class. - -[!code-csharp[using-static#1](../../../../samples/snippets/csharp/language-reference/keywords/using/using-static1.cs#1)] + +[!code-csharp[using-static#1](~/samples/snippets/csharp/language-reference/keywords/using/using-static1.cs#1)] By eliminating the need to explicitly reference the class each time a member is referenced, the `using static` directive produces much cleaner code: -[!code-csharp[using-static#2](../../../../samples/snippets/csharp/language-reference/keywords/using/using-static2.cs#1)] +[!code-csharp[using-static#2](~/samples/snippets/csharp/language-reference/keywords/using/using-static2.cs#1)] + +`using static` imports only accessible static members and nested types declared in the specified type. Inherited members are not imported. You can import from any named type with a using static directive, including Visual Basic modules. If F# top-level functions appear in metadata as static members of a named type whose name is a valid C# identifier, then the F# functions can be imported. + + `using static` makes extension methods declared in the specified type available for extension method lookup. However, the names of the extension methods are not imported into scope for unqualified reference in code. + + Methods with the same name imported from different types by different `using static` directives in the same compilation unit or namespace form a method group. Overload resolution within these method groups follows normal C# rules. -`using static` imports only accessible static members and nested types declared in the specified type. Inherited members are not imported. You can import from any named type with a using static directive, including Visual Basic modules. If F# top-level functions appear in metadata as static members of a named type whose name is a valid C# identifier, then the F# functions can be imported. - - `using static` makes extension methods declared in the specified type available for extension method lookup. However, the names of the extension methods are not imported into scope for unqualified reference in code. - - Methods with the same name imported from different types by different `using static` directives in the same compilation unit or namespace form a method group. Overload resolution within these method groups follows normal C# rules. - ## Example The following example uses the `using static` directive to make the static members of the , , and classes available without having to specify their type name. -[!code-csharp[using-static#3](../../../../samples/snippets/csharp/language-reference/keywords/using/using-static3.cs)] +[!code-csharp[using-static#3](~/samples/snippets/csharp/language-reference/keywords/using/using-static3.cs)] In the example, the `using static` directive could also have been applied to the type. This would have made it possible to call the method without specifying a type name. However, this creates less readable code, since it becomes necessary to check the `using static` statements to determine which numeric type's `TryParse` method is called. ## See also - [using directive](using-directive.md) -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Keywords](../../../csharp/language-reference/keywords/index.md) -- [Using Namespaces](../../../csharp/programming-guide/namespaces/using-namespaces.md) -- [Namespace Keywords](../../../csharp/language-reference/keywords/namespace-keywords.md) -- [Namespaces](../../../csharp/programming-guide/namespaces/index.md) +- [C# Reference](../index.md) +- [C# Keywords](index.md) +- [Using Namespaces](../../programming-guide/namespaces/using-namespaces.md) +- [Namespace Keywords](namespace-keywords.md) +- [Namespaces](../../programming-guide/namespaces/index.md) diff --git a/docs/csharp/language-reference/keywords/using.md b/docs/csharp/language-reference/keywords/using.md index d697c4a4ac2c7..afb009efac927 100644 --- a/docs/csharp/language-reference/keywords/using.md +++ b/docs/csharp/language-reference/keywords/using.md @@ -1,5 +1,5 @@ --- -title: "using (C# Reference)" +title: "using keyword (C# Reference)" ms.date: 07/20/2015 f1_keywords: - "using_CSharpKeyword" @@ -9,19 +9,20 @@ helpviewer_keywords: ms.assetid: 124e1a63-2a4b-4132-b269-3b6d8d3ef72d --- # using (C# Reference) -The `using` keyword has two major uses: - -- As a directive, when it is used to create an alias for a namespace or to import types defined in other namespaces. See [using Directive](../../../csharp/language-reference/keywords/using-directive.md). - -- As a statement, when it defines a scope at the end of which an object will be disposed. See [using Statement](../../../csharp/language-reference/keywords/using-statement.md). - + +The `using` keyword has two major uses: + +- As a directive, when it is used to create an alias for a namespace or to import types defined in other namespaces. See [using directive](using-directive.md). + +- As a statement, when it defines a scope at the end of which an object will be disposed. See [using statement](using-statement.md). + In addition, the [using static](using-static.md) directive lets you define a type whose static members you can access without specifying a type name. -## See Also +## See also -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Programming Guide](../../../csharp/programming-guide/index.md) -- [C# Keywords](../../../csharp/language-reference/keywords/index.md) -- [Namespace Keywords](../../../csharp/language-reference/keywords/namespace-keywords.md) -- [Namespaces](../../../csharp/programming-guide/namespaces/index.md) -- [extern](../../../csharp/language-reference/keywords/extern.md) +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [C# Keywords](index.md) +- [Namespace Keywords](namespace-keywords.md) +- [Namespaces](../../programming-guide/namespaces/index.md) +- [extern](extern.md) \ No newline at end of file From e9056e576c9d1acd753eba550056ce78e85cb621 Mon Sep 17 00:00:00 2001 From: Maira Wenzel Date: Tue, 4 Dec 2018 10:12:51 -0800 Subject: [PATCH 2/4] use sentence case for headings --- docs/csharp/language-reference/keywords/ushort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/language-reference/keywords/ushort.md b/docs/csharp/language-reference/keywords/ushort.md index 1e84ce0db5dc3..8ef08ec34f1c0 100644 --- a/docs/csharp/language-reference/keywords/ushort.md +++ b/docs/csharp/language-reference/keywords/ushort.md @@ -96,11 +96,11 @@ For information about arithmetic expressions with mixed floating-point types and For more information about implicit numeric conversion rules, see the [Implicit Numeric Conversions Table](implicit-numeric-conversions-table.md). -## C# Language Specification +## C# language specification For more information, see [Integral types](~/_csharplang/spec/types.md#integral-types) in the [C# Language Specification](../language-specification/index.md). The language specification is the definitive source for C# syntax and usage. -## See Also +## See also - - [C# Reference](../index.md) From 51a7166429debfcb146e17d55ba00be06e4f0528 Mon Sep 17 00:00:00 2001 From: Maira Wenzel Date: Tue, 4 Dec 2018 10:14:43 -0800 Subject: [PATCH 3/4] small changes --- .../csharp/language-reference/keywords/using-directive.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/csharp/language-reference/keywords/using-directive.md b/docs/csharp/language-reference/keywords/using-directive.md index 8c2c735ccefcf..cba87084b04f4 100644 --- a/docs/csharp/language-reference/keywords/using-directive.md +++ b/docs/csharp/language-reference/keywords/using-directive.md @@ -1,11 +1,11 @@ --- -title: "using Directive (C# Reference)" +title: "using directive (C# Reference)" ms.date: 07/20/2015 helpviewer_keywords: - "using directive [C#]" ms.assetid: b42b8e61-5e7e-439c-bb71-370094b44ae8 --- -# using Directive (C# Reference) +# using directive (C# Reference) The `using` directive has three uses: @@ -77,7 +77,7 @@ The following example shows how to define and use a `using` alias for a namespac [!code-csharp[csrefKeywordsNamespace#8](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsNamespace/CS/csrefKeywordsNamespace2.cs#8)] -A using alias directive cannot have an open generic type on the right hand side. For example, you cannot create a using alias for a List\, but you can create one for a `List`. +A using alias directive cannot have an open generic type on the right hand side. For example, you cannot create a using alias for a `List`, but you can create one for a `List`. ## Example 2 @@ -97,4 +97,4 @@ For more information, see [Using directives](~/_csharplang/spec/namespaces.md#us - [C# Keywords](index.md) - [Namespace Keywords](namespace-keywords.md) - [Namespaces](../../programming-guide/namespaces/index.md) -- [using Statement](using-statement.md) \ No newline at end of file +- [using Statement](using-statement.md) From 2c529f4095bcc21e922355f3b567268dd7ae04ca Mon Sep 17 00:00:00 2001 From: Maira Wenzel Date: Mon, 10 Dec 2018 00:46:03 -0800 Subject: [PATCH 4/4] sentence case for heading --- docs/csharp/language-reference/keywords/using-static.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/language-reference/keywords/using-static.md b/docs/csharp/language-reference/keywords/using-static.md index 72500260fc33c..c8ef8469b8101 100644 --- a/docs/csharp/language-reference/keywords/using-static.md +++ b/docs/csharp/language-reference/keywords/using-static.md @@ -1,5 +1,5 @@ --- -title: "using static Directive (C# Reference)" +title: "using static directive (C# Reference)" ms.date: 03/10/2017 helpviewer_keywords: - "using static directive [C#]" @@ -7,7 +7,7 @@ ms.assetid: 8b8f9e34-c75e-469b-ba85-6f2eb4090314 author: "rpetrusha" ms.author: "ronpet" --- -# using static Directive (C# Reference) +# using static directive (C# Reference) The `using static` directive designates a type whose static members and nested types you can access without specifying a type name. Its syntax is: