This commit is contained in:
Genevieve Warren 2021-04-22 20:50:13 -07:00 коммит произвёл GitHub
Родитель 723c119555
Коммит 63e9b2db00
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 153 добавлений и 174 удалений

Просмотреть файл

@ -254,7 +254,7 @@ The following example uses both approaches to include reserved characters in a c
### Floating-Point infinities and NaN
Regardless of the format string, if the value of a <xref:System.Single> or <xref:System.Double> floating-point type is positive infinity, negative infinity, or not a number (NaN), the formatted string is the value of the respective <xref:System.Globalization.NumberFormatInfo.PositiveInfinitySymbol%2A>, <xref:System.Globalization.NumberFormatInfo.NegativeInfinitySymbol%2A>, or <xref:System.Globalization.NumberFormatInfo.NaNSymbol%2A> property specified by the currently applicable <xref:System.Globalization.NumberFormatInfo> object.
Regardless of the format string, if the value of a <xref:System.Half>, <xref:System.Single>, or <xref:System.Double> floating-point type is positive infinity, negative infinity, or not a number (NaN), the formatted string is the value of the respective <xref:System.Globalization.NumberFormatInfo.PositiveInfinitySymbol%2A>, <xref:System.Globalization.NumberFormatInfo.NegativeInfinitySymbol%2A>, or <xref:System.Globalization.NumberFormatInfo.NaNSymbol%2A> property specified by the currently applicable <xref:System.Globalization.NumberFormatInfo> object.
### Control Panel settings

Просмотреть файл

@ -200,6 +200,7 @@ The general ("G") format specifier converts a number to the more compact of eith
|<xref:System.Int64>|19 digits|
|<xref:System.UInt64>|20 digits|
|<xref:System.Numerics.BigInteger>|Unlimited (same as ["R"](#RFormatString))|
|<xref:System.Half>|3 digits|
|<xref:System.Single>|7 digits|
|<xref:System.Double>|15 digits|
|<xref:System.Decimal>|29 digits|
@ -210,12 +211,9 @@ However, if the number is a <xref:System.Decimal> and the precision specifier is
If scientific notation is used, the exponent in the result is prefixed with "E" if the format specifier is "G", or "e" if the format specifier is "g". The exponent contains a minimum of two digits. This differs from the format for scientific notation that is produced by the exponential format specifier, which includes a minimum of three digits in the exponent.
Note that, when used with a <xref:System.Double> value, the "G17" format specifier ensures that the original <xref:System.Double> value successfully round-trips. This is because <xref:System.Double> is an IEEE 754-2008-compliant double-precision (`binary64`) floating point number that gives up to 17 significant digits of precision. We recommend its use instead of the ["R" format specifier](#RFormatString), since in some cases "R" fails to successfully round-trip double-precision floating point values. The following example illustrates one such case.
When used with a <xref:System.Double> value, the "G17" format specifier ensures that the original <xref:System.Double> value successfully round-trips. This is because <xref:System.Double> is an IEEE 754-2008-compliant double-precision (`binary64`) floating-point number that gives up to 17 significant digits of precision. On .NET Framework, we recommend its use instead of the ["R" format specifier](#RFormatString), since in some cases "R" fails to successfully round-trip double-precision floating point values.
[!code-csharp-interactive[Round-tripping a Double](../../../samples/snippets/standard/base-types/format-strings/csharp/g17.cs#GeneralFormatSpecifier)]
[!code-vb[Round-tripping a Double](../../../samples/snippets/standard/base-types/format-strings/vb/g17.vb)]
When used with a <xref:System.Single> value, the "G9" format specifier ensures that the original <xref:System.Single> value successfully round-trips. This is because <xref:System.Single> is an IEEE 754-2008-compliant single-precision (`binary32`) floating point number that gives up to nine significant digits of precision. For performance reasons, we recommend its use instead of the ["R" format specifier](#RFormatString).
When used with a <xref:System.Single> value, the "G9" format specifier ensures that the original <xref:System.Single> value successfully round-trips. This is because <xref:System.Single> is an IEEE 754-2008-compliant single-precision (`binary32`) floating-point number that gives up to nine significant digits of precision. For performance reasons, we recommend its use instead of the ["R" format specifier](#RFormatString).
The result string is affected by the formatting information of the current <xref:System.Globalization.NumberFormatInfo> object. The following table lists the <xref:System.Globalization.NumberFormatInfo> properties that control the formatting of the result string.
@ -283,7 +281,7 @@ The following example formats floating-point values with the percent format spec
## Round-trip format specifier (R)
The round-trip ("R") format specifier attempts to ensure that a numeric value that is converted to a string is parsed back into the same numeric value. This format is supported only for the <xref:System.Single>, <xref:System.Double>, and <xref:System.Numerics.BigInteger> types.
The round-trip ("R") format specifier attempts to ensure that a numeric value that is converted to a string is parsed back into the same numeric value. This format is supported only for the <xref:System.Half>, <xref:System.Single>, <xref:System.Double>, and <xref:System.Numerics.BigInteger> types.
For <xref:System.Double> values, the "R" format specifier in some cases fails to successfully round-trip the original value. For both <xref:System.Double> and <xref:System.Single> values, it also offers relatively poor performance. Instead, we recommend that you use the ["G17"](#GFormatString) format specifier for <xref:System.Double> values and the ["G9"](#GFormatString) format specifier to successfully round-trip <xref:System.Single> values.
@ -347,15 +345,15 @@ Formatting is influenced by the properties of the current <xref:System.Globaliza
### Integral and floating-point numeric types
Some descriptions of standard numeric format specifiers refer to integral or floating-point numeric types. The integral numeric types are <xref:System.Byte>, <xref:System.SByte>, <xref:System.Int16>, <xref:System.Int32>, <xref:System.Int64>, <xref:System.UInt16>, <xref:System.UInt32>, <xref:System.UInt64>, and <xref:System.Numerics.BigInteger>. The floating-point numeric types are <xref:System.Decimal>, <xref:System.Single>, and <xref:System.Double>.
Some descriptions of standard numeric format specifiers refer to integral or floating-point numeric types. The integral numeric types are <xref:System.Byte>, <xref:System.SByte>, <xref:System.Int16>, <xref:System.Int32>, <xref:System.Int64>, <xref:System.UInt16>, <xref:System.UInt32>, <xref:System.UInt64>, and <xref:System.Numerics.BigInteger>. The floating-point numeric types are <xref:System.Decimal>, <xref:System.Half>, <xref:System.Single>, and <xref:System.Double>.
### Floating-point infinities and NaN
Regardless of the format string, if the value of a <xref:System.Single> or <xref:System.Double> floating-point type is positive infinity, negative infinity, or not a number (NaN), the formatted string is the value of the respective <xref:System.Globalization.NumberFormatInfo.PositiveInfinitySymbol%2A>, <xref:System.Globalization.NumberFormatInfo.NegativeInfinitySymbol%2A>, or <xref:System.Globalization.NumberFormatInfo.NaNSymbol%2A> property that is specified by the currently applicable <xref:System.Globalization.NumberFormatInfo> object.
Regardless of the format string, if the value of a <xref:System.Half>, <xref:System.Single>, or <xref:System.Double> floating-point type is positive infinity, negative infinity, or not a number (NaN), the formatted string is the value of the respective <xref:System.Globalization.NumberFormatInfo.PositiveInfinitySymbol%2A>, <xref:System.Globalization.NumberFormatInfo.NegativeInfinitySymbol%2A>, or <xref:System.Globalization.NumberFormatInfo.NaNSymbol%2A> property that is specified by the currently applicable <xref:System.Globalization.NumberFormatInfo> object.
## Code example
The following example formats an integral and a floating-point numeric value using the en-US culture and all the standard numeric format specifiers. This example uses two particular numeric types (<xref:System.Double> and <xref:System.Int32>), but would yield similar results for any of the other numeric base types (<xref:System.Byte>, <xref:System.SByte>, <xref:System.Int16>, <xref:System.Int32>, <xref:System.Int64>, <xref:System.UInt16>, <xref:System.UInt32>, <xref:System.UInt64>, <xref:System.Numerics.BigInteger>, <xref:System.Decimal>, and <xref:System.Single>).
The following example formats an integral and a floating-point numeric value using the en-US culture and all the standard numeric format specifiers. This example uses two particular numeric types (<xref:System.Double> and <xref:System.Int32>), but would yield similar results for any of the other numeric base types (<xref:System.Byte>, <xref:System.SByte>, <xref:System.Int16>, <xref:System.Int32>, <xref:System.Int64>, <xref:System.UInt16>, <xref:System.UInt32>, <xref:System.UInt64>, <xref:System.Numerics.BigInteger>, <xref:System.Decimal>, <xref:System.Half>, and <xref:System.Single>).
[!code-csharp[system.x.tostring-and-culture#1](../../../samples/snippets/csharp/VS_Snippets_CLR_System/system.X.ToString-and-Culture/cs/xts.cs#FinalExample)]
[!code-vb[system.x.tostring-and-culture#1](../../../samples/snippets/visualbasic/VS_Snippets_CLR_System/system.X.ToString-and-Culture/vb/xts.vb#1)]

Просмотреть файл

@ -1,8 +1,8 @@
---
title: ".NET class library overview"
title: .NET class library overview
description: Learn about the .NET class library. .NET APIs include classes, interfaces, delegates, and value types to provide access to system functionality.
ms.date: "02/08/2018"
helpviewer_keywords:
ms.date: 04/22/2021
helpviewer_keywords:
- "classes [.NET], library overview"
- ".NET, library overview"
- "class objects value type"
@ -38,68 +38,69 @@ ms.assetid: 7e4c5921-955d-4b06-8709-101873acf157
---
# .NET class library overview
.NET APIs include classes, interfaces, delegates, and value types that expedite and optimize the development process and provide access to system functionality. To facilitate interoperability between languages, most .NET types are CLS-compliant and can therefore be used from any programming language whose compiler conforms to the common language specification (CLS).
.NET types are the foundation on which .NET applications, components, and controls are built. .NET includes types that perform the following functions:
- Represent base data types and exceptions.
- Encapsulate data structures.
- Perform I/O.
- Access information about loaded types.
- Invoke .NET security checks.
- Provide data access, rich client-side GUI, and server-controlled, client-side GUI.
.NET provides a rich set of interfaces, as well as abstract and concrete (non-abstract) classes. You can use the concrete classes as-is or, in many cases, derive your own classes from them. To use the functionality of an interface, you can either create a class that implements the interface or derive a class from one of the .NET classes that implements the interface.
.NET APIs include classes, interfaces, delegates, and value types that expedite and optimize the development process and provide access to system functionality. To facilitate interoperability between languages, most .NET types are CLS-compliant and can therefore be used from any programming language whose compiler conforms to the common language specification (CLS).
.NET types are the foundation on which .NET applications, components, and controls are built. .NET includes types that perform the following functions:
- Represent base data types and exceptions.
- Encapsulate data structures.
- Perform I/O.
- Access information about loaded types.
- Invoke .NET security checks.
- Provide data access, rich client-side GUI, and server-controlled, client-side GUI.
.NET provides a rich set of interfaces, as well as abstract and concrete (non-abstract) classes. You can use the concrete classes as-is or, in many cases, derive your own classes from them. To use the functionality of an interface, you can either create a class that implements the interface or derive a class from one of the .NET classes that implements the interface.
## Naming conventions
.NET types use a dot syntax naming scheme that connotes a hierarchy. This technique groups related types into namespaces so they can be searched and referenced more easily. The first part of the full name — up to the rightmost dot — is the namespace name. The last part of the name is the type name. For example, `System.Collections.Generic.List<T>` represents the `List<T>` type, which belongs to the `System.Collections.Generic` namespace. The types in <xref:System.Collections.Generic> can be used to work with generic collections.
This naming scheme makes it easy for library developers extending .NET to create hierarchical groups of types and name them in a consistent, informative manner. It also allows types to be unambiguously identified by their full name (that is, by their namespace and type name), which prevents type name collisions. Library developers are expected to use the following convention when creating names for their namespaces:
*CompanyName*.*TechnologyName*
For example, the namespace `Microsoft.Word` conforms to this guideline.
The use of naming patterns to group related types into namespaces is a useful way to build and document class libraries. However, this naming scheme has no effect on visibility, member access, inheritance, security, or binding. A namespace can be partitioned across multiple assemblies and a single assembly can contain types from multiple namespaces. The assembly provides the formal structure for versioning, deployment, security, loading, and visibility in the common language runtime.
For more information on namespaces and type names, see [Common Type System](base-types/common-type-system.md).
.NET types use a dot syntax naming scheme that connotes a hierarchy. This technique groups related types into namespaces so they can be searched and referenced more easily. The first part of the full name&mdash;up to the rightmost dot&mdash;is the namespace name. The last part of the name is the type name. For example, `System.Collections.Generic.List<T>` represents the `List<T>` type, which belongs to the `System.Collections.Generic` namespace. The types in <xref:System.Collections.Generic> can be used to work with generic collections.
This naming scheme makes it easy for library developers extending .NET to create hierarchical groups of types and name them in a consistent, informative manner. It also allows types to be unambiguously identified by their full name (that is, by their namespace and type name), which prevents type name collisions. Library developers are expected to use the following convention when creating names for their namespaces:
*CompanyName*.*TechnologyName*
For example, the namespace `Microsoft.Word` conforms to this guideline.
The use of naming patterns to group related types into namespaces is a useful way to build and document class libraries. However, this naming scheme has no effect on visibility, member access, inheritance, security, or binding. A namespace can be partitioned across multiple assemblies and a single assembly can contain types from multiple namespaces. The assembly provides the formal structure for versioning, deployment, security, loading, and visibility in the common language runtime.
For more information on namespaces and type names, see [Common Type System](base-types/common-type-system.md).
## System namespace
The <xref:System> namespace is the root namespace for fundamental types in .NET. This namespace includes classes that represent the base data types used by all applications: <xref:System.Object> (the root of the inheritance hierarchy), <xref:System.Byte>, <xref:System.Char>, <xref:System.Array>, <xref:System.Int32>, <xref:System.String>, and so on. Many of these types correspond to the primitive data types that your programming language uses. When you write code using .NET types, you can use your language's corresponding keyword when a .NET base data type is expected.
The following table lists the base types that .NET supplies, briefly describes each type, and indicates the corresponding type in Visual Basic, C#, C++, and F#.
|Category|Class name|Description|Visual Basic data type|C# data type|C++/CLI data type|F# data type|
|--------------|----------------|-----------------|----------------------------|-------------------|---------------------|-----------------------|
|Integer|<xref:System.Byte>|An 8-bit unsigned integer.|**Byte**|**byte**|**unsigned char**|**byte**|
||<xref:System.SByte>|An 8-bit signed integer.<br /><br /> Not CLS-compliant.|**SByte**|**sbyte**|**char**<br /> -or-<br /> **signed** **char**|**sbyte**|
||<xref:System.Int16>|A 16-bit signed integer.|**Short**|**short**|**short**|**int16**|
||<xref:System.Int32>|A 32-bit signed integer.|**Integer**|**int**|**int**<br /><br /> -or-<br /><br /> **long**|**int**|
||<xref:System.Int64>|A 64-bit signed integer.|**Long**|**long**|**__int64**|**int64**|
||<xref:System.UInt16>|A 16-bit unsigned integer.<br /><br /> Not CLS-compliant.|**UShort**|**ushort**|**unsigned short**|**uint16**|
||<xref:System.UInt32>|A 32-bit unsigned integer.<br /><br /> Not CLS-compliant.|**UInteger**|**uint**|**unsigned int**<br /> -or-<br /> **unsigned long**|**uint32**|
||<xref:System.UInt64>|A 64-bit unsigned integer.<br /><br /> Not CLS-compliant.|**ULong**|**ulong**|**unsigned __int64**|**uint64**|
|Floating point|<xref:System.Single>|A single-precision (32-bit) floating-point number.|**Single**|**float**|**float**|**float32**<br> or<br>**single**|
||<xref:System.Double>|A double-precision (64-bit) floating-point number.|**Double**|**double**|**double**|**float**<br> or <br> **double**|
|Logical|<xref:System.Boolean>|A Boolean value (true or false).|**Boolean**|**bool**|**bool**|**bool**|
|Other|<xref:System.Char>|A Unicode (16-bit) character.|**Char**|**char**|**wchar_t**|**char**|
||<xref:System.Decimal>|A decimal (128-bit) value.|**Decimal**|**decimal**|**Decimal**|**decimal**|
||<xref:System.IntPtr>|A signed integer whose size depends on the underlying platform (a 32-bit value on a 32-bit platform and a 64-bit value on a 64-bit platform).|**IntPtr**<br /><br /> No built-in type.|**IntPtr**<br /><br /> No built-in type.|**IntPtr**<br /><br /> No built-in type.|**unativeint**|
||<xref:System.UIntPtr>|An unsigned integer whose size depends on the underlying platform (a 32- bit value on a 32-bit platform and a 64-bit value on a 64-bit platform).<br /><br /> Not CLS-compliant.|**UIntPtr**<br /><br /> No built-in type.|**UIntPtr**<br /><br /> No built-in type.|**UIntPtr**<br /><br /> No built-in type.|**unativeint**|
||<xref:System.Object>|The root of the object hierarchy.|**Object**|**object**|**Object^**|**obj**|
||<xref:System.String>|An immutable, fixed-length string of Unicode characters.|**String**|**string**|**String^**|**string**|
In addition to the base data types, the <xref:System> namespace contains over 100 classes, ranging from classes that handle exceptions to classes that deal with core runtime concepts, such as application domains and the garbage collector. The <xref:System> namespace also contains many second-level namespaces.
For more information about namespaces, use the [.NET API Browser](../../api/index.md) to browse the .NET Class Library. The API reference documentation provides documentation on each namespace, its types, and each of their members.
The <xref:System> namespace is the root namespace for fundamental types in .NET. This namespace includes classes that represent the base data types used by all applications, for example, <xref:System.Object> (the root of the inheritance hierarchy), <xref:System.Byte>, <xref:System.Char>, <xref:System.Array>, <xref:System.Int32>, and <xref:System.String>. Many of these types correspond to the primitive data types that your programming language uses. When you write code using .NET types, you can use your language's corresponding keyword when a .NET base data type is expected.
The following table lists the base types that .NET supplies, briefly describes each type, and indicates the corresponding type in Visual Basic, C#, C++, and F#.
|Category|Class name|Description|Visual Basic data type|C# data type|C++/CLI data type|F# data type|
|--------------|----------------|-----------------|----------------------------|-------------------|---------------------|-----------------------|
|Integer|<xref:System.Byte>|An 8-bit unsigned integer.|`Byte`|`byte`|`unsigned char`|`byte`|
||<xref:System.SByte>|An 8-bit signed integer.<br /><br /> Not CLS-compliant.|`SByte`|`sbyte`|`char` or `signed char`|`sbyte`|
||<xref:System.Int16>|A 16-bit signed integer.|`Short`|`short`|`short`|`int16`|
||<xref:System.Int32>|A 32-bit signed integer.|`Integer`|`int`|`int` or `long`|`int`|
||<xref:System.Int64>|A 64-bit signed integer.|`Long`|`long`|`__int64`|`int64`|
||<xref:System.UInt16>|A 16-bit unsigned integer.<br /><br /> Not CLS-compliant.|`UShort`|`ushort`|`unsigned short`|`uint16`|
||<xref:System.UInt32>|A 32-bit unsigned integer.<br /><br /> Not CLS-compliant.|`UInteger`|`uint`|`unsigned int` or `unsigned long`|`uint32`|
||<xref:System.UInt64>|A 64-bit unsigned integer.<br /><br /> Not CLS-compliant.|`ULong`|`ulong`|`unsigned __int64`|`uint64`|
|Floating point|<xref:System.Half>|A half-precision (16-bit) floating-point number.|||||
||<xref:System.Single>|A single-precision (32-bit) floating-point number.|`Single`|`float`|`float`|`float32` or `single`|
||<xref:System.Double>|A double-precision (64-bit) floating-point number.|`Double`|`double`|`double`|`float` or `double`|
|Logical|<xref:System.Boolean>|A Boolean value (true or false).|`Boolean`|`bool`|`bool`|`bool`|
|Other|<xref:System.Char>|A Unicode (16-bit) character.|`Char`|`char`|`wchar_t`|`char`|
||<xref:System.Decimal>|A decimal (128-bit) value.|`Decimal`|`decimal`|`Decimal`|`decimal`|
||<xref:System.IntPtr>|A signed integer whose size depends on the underlying platform (a 32-bit value on a 32-bit platform and a 64-bit value on a 64-bit platform).| |`nint`| |`unativeint`|
||<xref:System.UIntPtr>|An unsigned integer whose size depends on the underlying platform (a 32- bit value on a 32-bit platform and a 64-bit value on a 64-bit platform).<br /><br /> Not CLS-compliant.| |`nuint`| |`unativeint`|
||<xref:System.Object>|The root of the object hierarchy.|`Object`|`object`|`Object^`|`obj`|
||<xref:System.String>|An immutable, fixed-length string of Unicode characters.|`String`|`string`|`String^`|`string`|
In addition to the base data types, the <xref:System> namespace contains over 100 classes, ranging from classes that handle exceptions to classes that deal with core runtime concepts, such as application domains and the garbage collector. The <xref:System> namespace also contains many second-level namespaces.
For more information about namespaces, use the [.NET API Browser](../../api/index.md) to browse the .NET Class Library. The API reference documentation provides documentation on each namespace, its types, and each of their members.
## See also
- [Common Type System](base-types/common-type-system.md)

Просмотреть файл

@ -17,7 +17,7 @@ The BCL provides the most foundational types and utility functionality and are t
## Primitive Types
.NET includes a set of primitive types, which are used (to varying degrees) in all programs. These types contain data, such as numbers, strings, bytes and arbitrary objects. The C# language includes keywords for these types. A sample set of these types is listed below, with the matching C# keywords.
.NET includes a set of primitive types, which are used (to varying degrees) in all programs. These types contain data, such as numbers, strings, bytes, and arbitrary objects. The C# language includes keywords for these types. A sample set of these types is listed below, with the matching C# keywords.
* <xref:System.Object?displayProperty=nameWithType> ([object](../csharp/language-reference/builtin-types/reference-types.md#the-object-type)) - The ultimate base class in the CLR type system. It is the root of the type hierarchy.
* <xref:System.Int16?displayProperty=nameWithType> ([short](../csharp/language-reference/builtin-types/integral-numeric-types.md)) - A 16-bit signed integer type. The unsigned <xref:System.UInt16> also exists.

Просмотреть файл

@ -70,7 +70,7 @@ In this article:
This section discusses the rules for creating a CLS-compliant component. For a complete list of rules, see Partition I, Clause 11 of the [ECMA-335 Standard: Common Language Infrastructure](https://www.ecma-international.org/publications-and-standards/standards/ecma-335/).
> [!NOTE]
> The Common Language Specification discusses each rule for CLS compliance as it applies to consumers (developers who are programmatically accessing a component that is CLS-compliant), frameworks (developers who are using a language compiler to create CLS-compliant libraries), and extenders (developers who are creating a tool such as a language compiler or a code parser that creates CLS-compliant components). This article focuses on the rules as they apply to frameworks. Note, though, that some of the rules that apply to extenders may also apply to assemblies that are created using Reflection.Emit.
> The Common Language Specification discusses each rule for CLS compliance as it applies to consumers (developers who are programmatically accessing a component that is CLS-compliant), frameworks (developers who are using a language compiler to create CLS-compliant libraries), and extenders (developers who are creating a tool such as a language compiler or a code parser that creates CLS-compliant components). This article focuses on the rules as they apply to frameworks. Note, though, that some of the rules that apply to extenders may also apply to assemblies that are created using `Reflection.Emit`.
To design a component that is language independent, you only need to apply the rules for CLS compliance to your component's public interface. Your private implementation does not have to conform to the specification.
@ -172,6 +172,7 @@ The .NET [common type system](base-types/common-type-system.md) includes a numbe
|<xref:System.Int16>|16-bit signed integer|
|<xref:System.Int32>|32-bit signed integer|
|<xref:System.Int64>|64-bit signed integer|
|<xref:System.Half>|Half-precision floating-point value|
|<xref:System.Single>|Single-precision floating-point value|
|<xref:System.Double>|Double-precision floating-point value|
|<xref:System.Boolean>|`true` or `false` value type|
@ -382,7 +383,7 @@ If constraints are applied to a generic type, any types used as constraints must
[!code-csharp[Conceptual.CLSCompliant#34](../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.clscompliant/cs/generics5.cs#34)]
[!code-vb[Conceptual.CLSCompliant#34](../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.clscompliant/vb/generics5.vb#34)]
If a generic type is derived from a generic base type, it must redeclare any constraints so that it can guarantee that constraints on the base type are also satisfied. The following example defines a `Number<T>` that can represent any numeric type. It also defines a `FloatingPoint<T>` class that represents a floating point value. However, the source code fails to compile, because it does not apply the constraint on `Number<T>` (that T must be a value type) to `FloatingPoint<T>`.
If a generic type is derived from a generic base type, it must redeclare any constraints so that it can guarantee that constraints on the base type are also satisfied. The following example defines a `Number<T>` that can represent any numeric type. It also defines a `FloatingPoint<T>` class that represents a floating-point value. However, the source code fails to compile, because it does not apply the constraint on `Number<T>` (that T must be a value type) to `FloatingPoint<T>`.
[!code-csharp[Conceptual.CLSCompliant#30](../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.clscompliant/cs/generics2a.cs#30)]
[!code-vb[Conceptual.CLSCompliant#30](../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.clscompliant/vb/generics2a.vb#30)]

Просмотреть файл

@ -295,6 +295,7 @@ CLS-compliant type | Description
[Int16](xref:System.Int16) | 16-bit signed integer
[Int32](xref:System.Int32) | 32-bit signed integer
[Int64](xref:System.Int64) | 64-bit signed integer
[Half](xref:System.Half) | Half-precision floating-point value
[Single](xref:System.Single) | Single-precision floating-point value
[Double](xref:System.Double) | Double-precision floating-point value
[Boolean](xref:System.Boolean) | true or false value type
@ -317,27 +318,27 @@ The .NET Class Library or any other class library may include other types that a
* Boxed value types. The following C# example creates a class that has a public property of type `int*` named `Value`. Because an `int*` is a boxed value type, the compiler flags it as non-CLS-compliant.
```csharp
using System;
```csharp
using System;
[assembly:CLSCompliant(true)]
[assembly:CLSCompliant(true)]
public unsafe class TestClass
{
private int* val;
public unsafe class TestClass
{
private int* val;
public TestClass(int number)
{
val = (int*) number;
}
public TestClass(int number)
{
val = (int*) number;
}
public int* Value {
get { return val; }
}
}
// The compiler generates the following output when compiling this example:
// warning CS3003: Type of 'TestClass.Value' is not CLS-compliant
```
public int* Value {
get { return val; }
}
}
// The compiler generates the following output when compiling this example:
// warning CS3003: Type of 'TestClass.Value' is not CLS-compliant
```
* Typed references, which are special constructs that contain a reference to an object and a reference to a type.
@ -1628,7 +1629,7 @@ End Class
' ~~~~~~~~~
```
If a generic type is derived from a generic base type, it must redeclare any constraints so that it can guarantee that constraints on the base type are also satisfied. The following example defines a `Number<T>` that can represent any numeric type. It also defines a `FloatingPoint<T>` class that represents a floating point value. However, the source code fails to compile, because it does not apply the constraint on `Number<T>` (that T must be a value type) to `FloatingPoint<T>`.
If a generic type is derived from a generic base type, it must redeclare any constraints so that it can guarantee that constraints on the base type are also satisfied. The following example defines a `Number<T>` that can represent any numeric type. It also defines a `FloatingPoint<T>` class that represents a floating-point value. However, the source code fails to compile, because it does not apply the constraint on `Number<T>` (that T must be a value type) to `FloatingPoint<T>`.
```csharp
using System;
@ -1669,7 +1670,8 @@ public class FloatingPoint<T> : Number<T>
{
public FloatingPoint(T number) : base(number)
{
if (typeof(float) == number.GetType() ||
if (typeof(System.Half) == number.GetType() ||
typeof(float) == number.GetType() ||
typeof(double) == number.GetType() ||
typeof(decimal) == number.GetType())
this.number = Convert.ToDouble(number);
@ -1712,7 +1714,8 @@ End Class
Public Class FloatingPoint(Of T) : Inherits Number(Of T)
Public Sub New(number As T)
MyBase.New(number)
If TypeOf number Is Single Or
If TypeOf number Is System.Half Or
TypeOf number Is Single Or
TypeOf number Is Double Or
TypeOf number Is Decimal Then
Me.number = Convert.ToDouble(number)
@ -1770,7 +1773,8 @@ public class FloatingPoint<T> : Number<T> where T : struct
{
public FloatingPoint(T number) : base(number)
{
if (typeof(float) == number.GetType() ||
if (typeof(System.Half) == number.GetType() ||
typeof(float) == number.GetType() ||
typeof(double) == number.GetType() ||
typeof(decimal) == number.GetType())
this.number = Convert.ToDouble(number);
@ -1810,7 +1814,8 @@ End Class
Public Class FloatingPoint(Of T As Structure) : Inherits Number(Of T)
Public Sub New(number As T)
MyBase.New(number)
If TypeOf number Is Single Or
If TypeOf number Is System.Half Or
TypeOf number Is Single Or
TypeOf number Is Double Or
TypeOf number Is Decimal Then
Me.number = Convert.ToDouble(number)

Просмотреть файл

@ -2,7 +2,7 @@
description: "Learn more about: Numerics in .NET"
title: "Numerics in .NET"
ms.date: "10/18/2018"
helpviewer_keywords:
helpviewer_keywords:
- "SIMD"
- "System.Numerics.Vectors"
- "vectors"
@ -14,63 +14,77 @@ ms.assetid: dfebc18e-acde-4510-9fa7-9a0f4aa3bd11
---
# Numerics in .NET
.NET provides a range of numeric integer and floating-point primitives, as well as <xref:System.Numerics.BigInteger?displayProperty=nameWithType>, which is an integral type with no theoretical upper or lower bound, <xref:System.Numerics.Complex?displayProperty=nameWithType>, which represents complex numbers, and a set of SIMD-enabled types in the <xref:System.Numerics> namespace.
.NET provides a range of numeric integer and floating-point primitives, as well as:
- <xref:System.Numerics.BigInteger?displayProperty=nameWithType>, which is an integral type with no theoretical upper or lower bound.
- <xref:System.Numerics.Complex?displayProperty=nameWithType>, which represents complex numbers.
- A set of SIMD-enabled types in the <xref:System.Numerics> namespace.
## Integer types
.NET supports both signed and unsigned 8-, 16-, 32-, and 64-bit integer types, which are listed in the following table:
|Type|Signed/Unsigned|Size (in bytes)|Minimum value|Maximum value|
|----------|----------------------|--------------------|-------------------|-------------------|
|<xref:System.Byte?displayProperty=nameWithType>|Unsigned|1|0|255|
|<xref:System.Int16?displayProperty=nameWithType>|Signed|2|-32,768|32,767|
|<xref:System.Int32?displayProperty=nameWithType>|Signed|4|-2,147,483,648|2,147,483,647|
|<xref:System.Int64?displayProperty=nameWithType>|Signed|8|-9,223,372,036,854,775,808|9,223,372,036,854,775,807|
|<xref:System.SByte?displayProperty=nameWithType>|Signed|1|-128|127|
|<xref:System.UInt16?displayProperty=nameWithType>|Unsigned|2|0|65,535|
|<xref:System.UInt32?displayProperty=nameWithType>|Unsigned|4|0|4,294,967,295|
|<xref:System.UInt64?displayProperty=nameWithType>|Unsigned|8|0|18,446,744,073,709,551,615|
.NET supports both signed and unsigned 8-bit, 16-bit, 32-bit, and 64-bit integer types, which are listed in the following tables.
**Signed integer types**
|Type|Size (in bytes)|Minimum value|Maximum value|
|----------|--------------------|-------------------|-------------------|
|<xref:System.Int16?displayProperty=nameWithType>|2|-32,768|32,767|
|<xref:System.Int32?displayProperty=nameWithType>|4|-2,147,483,648|2,147,483,647|
|<xref:System.Int64?displayProperty=nameWithType>|8|-9,223,372,036,854,775,808|9,223,372,036,854,775,807|
|<xref:System.SByte?displayProperty=nameWithType>|1|-128|127|
|<xref:System.IntPtr?displayProperty=nameWithType>|4 (on 32-bit machine)<br/>8 (on 64-bit machine)|-9,223,372,036,854,775,808 (on 64-bit machine)|9,223,372,036,854,775,807 (on 64-bit machine)|
**Unsigned integer types**
|Type|Size (in bytes)|Minimum value|Maximum value|
|----------|--------------------|-------------------|-------------------|
|<xref:System.Byte?displayProperty=nameWithType>|1|0|255|
|<xref:System.UInt16?displayProperty=nameWithType>|2|0|65,535|
|<xref:System.UInt32?displayProperty=nameWithType>|4|0|4,294,967,295|
|<xref:System.UInt64?displayProperty=nameWithType>|8|0|18,446,744,073,709,551,615|
|<xref:System.UIntPtr?displayProperty=nameWithType>|4 (on 32-bit machine)<br/>8 (on 64-bit machine)|0|18,446,744,073,709,551,615 (on 64-bit machine)|
Each integer type supports a set of standard arithmetic operators. The <xref:System.Math?displayProperty=nameWithType> class provides methods for a broader set of mathematical functions.
You can also work with the individual bits in an integer value by using the <xref:System.BitConverter?displayProperty=nameWithType> class.
You can also work with the individual bits in an integer value by using the <xref:System.BitConverter?displayProperty=nameWithType> class.
> [!NOTE]
> [!NOTE]
> The unsigned integer types are not CLS-compliant. For more information, see [Language Independence and Language-Independent Components](language-independence-and-language-independent-components.md).
## BigInteger
The <xref:System.Numerics.BigInteger?displayProperty=nameWithType> structure is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds. The methods of the <xref:System.Numerics.BigInteger> type closely parallel those of the other integral types.
## Floating-point types
.NET includes three primitive floating-point types, which are listed in the following table:
|Type|Size (in bytes)|Approximate range|Precision|
|----------|--------|---------------------|--------------------|
|<xref:System.Single?displayProperty=nameWithType>|4|±1.5 x 10<sup>45</sup> to ±3.4 x 10<sup>38</sup>|~6-9 digits|
|<xref:System.Double?displayProperty=nameWithType>|8|±5.0 × 10<sup>324</sup> to ±1.7 × 10<sup>308</sup>|~15-17 digits|
|<xref:System.Decimal?displayProperty=nameWithType>|16|±1.0 x 10<sup>-28</sup> to ±7.9228 x 10<sup>28</sup>|28-29 digits|
Both <xref:System.Single> and <xref:System.Double> types support special values that represent not-a-number and infinity. For example, the <xref:System.Double> type provides the following values: <xref:System.Double.NaN?displayProperty=nameWithType>, <xref:System.Double.NegativeInfinity?displayProperty=nameWithType>, and <xref:System.Double.PositiveInfinity?displayProperty=nameWithType>. You use the <xref:System.Double.IsNaN%2A?displayProperty=nameWithType>, <xref:System.Double.IsInfinity%2A?displayProperty=nameWithType>, <xref:System.Double.IsPositiveInfinity%2A?displayProperty=nameWithType>, and <xref:System.Double.IsNegativeInfinity%2A?displayProperty=nameWithType> methods to test for these special values.
.NET includes the following primitive floating-point types:
|Type|Size (in bytes)|Approximate range|Notes|
|-|-|-|-|
|<xref:System.Half?displayProperty=nameWithType>|2|±65504|Introduced in .NET 5|
|<xref:System.Single?displayProperty=nameWithType>|4|±3.4 x 10<sup>38</sup>||
|<xref:System.Double?displayProperty=nameWithType>|8|±1.7 × 10<sup>308</sup>||
|<xref:System.Decimal?displayProperty=nameWithType>|16|±7.9228 x 10<sup>28</sup>||
The <xref:System.Half>, <xref:System.Single>, and <xref:System.Double> types support special values that represent not-a-number and infinity. For example, the <xref:System.Double> type provides the following values: <xref:System.Double.NaN?displayProperty=nameWithType>, <xref:System.Double.NegativeInfinity?displayProperty=nameWithType>, and <xref:System.Double.PositiveInfinity?displayProperty=nameWithType>. You use the <xref:System.Double.IsNaN%2A?displayProperty=nameWithType>, <xref:System.Double.IsInfinity%2A?displayProperty=nameWithType>, <xref:System.Double.IsPositiveInfinity%2A?displayProperty=nameWithType>, and <xref:System.Double.IsNegativeInfinity%2A?displayProperty=nameWithType> methods to test for these special values.
Each floating-point type supports a set of standard arithmetic operators. The <xref:System.Math?displayProperty=nameWithType> class provides methods for a broader set of mathematical functions. .NET Core 2.0 and later includes the <xref:System.MathF?displayProperty=nameWithType> class, which provides methods that accept arguments of the <xref:System.Single> type.
You can also work with the individual bits in <xref:System.Double> and <xref:System.Single> values by using the <xref:System.BitConverter?displayProperty=nameWithType> class. The <xref:System.Decimal?displayProperty=nameWithType> structure has its own methods, <xref:System.Decimal.GetBits%2A?displayProperty=nameWithType> and <xref:System.Decimal.%23ctor%28System.Int32%5B%5D%29>, for working with a decimal value's individual bits, as well as its own set of methods for performing some additional mathematical operations.
The <xref:System.Double> and <xref:System.Single> types are intended to be used for values that, by their nature, are imprecise (for example, the distance between two stars) and for applications in which a high degree of precision and small rounding error is not required. Use the <xref:System.Decimal?displayProperty=nameWithType> type for cases in which greater precision is required and rounding errors should be minimized.
You can also work with the individual bits in <xref:System.Double>, <xref:System.Single>, and <xref:System.Half> values by using the <xref:System.BitConverter?displayProperty=nameWithType> class. The <xref:System.Decimal?displayProperty=nameWithType> structure has its own methods, <xref:System.Decimal.GetBits%2A?displayProperty=nameWithType> and <xref:System.Decimal.%23ctor%28System.Int32%5B%5D%29>, for working with a decimal value's individual bits, as well as its own set of methods for performing some additional mathematical operations.
The <xref:System.Double>, <xref:System.Single>, and <xref:System.Half> types are intended to be used for values that, by their nature, are imprecise (for example, the distance between two stars) and for applications in which a high degree of precision and small rounding error is not required. Use the <xref:System.Decimal?displayProperty=nameWithType> type for cases in which greater precision is required and rounding errors should be minimized.
> [!NOTE]
> The <xref:System.Decimal> type doesn't eliminate the need for rounding. Rather, it minimizes errors due to rounding.
## Complex
The <xref:System.Numerics.Complex?displayProperty=nameWithType> structure represents a complex number, that is, a number with a real number part and an imaginary number part. It supports a standard set of arithmetic, comparison, equality, explicit and implicit conversion operators, as well as mathematical, algebraic, and trigonometric methods.
The <xref:System.Numerics.Complex?displayProperty=nameWithType> structure represents a complex number, that is, a number with a real number part and an imaginary number part. It supports a standard set of arithmetic, comparison, equality, explicit and implicit conversion operators, as well as mathematical, algebraic, and trigonometric methods.
## SIMD-enabled types
The <xref:System.Numerics> namespace includes a set of .NET SIMD-enabled types. SIMD (Single Instruction Multiple Data) operations can be parallelized at the hardware level. That increases the throughput of the vectorized computations, which are common in mathematical, scientific, and graphics apps.
The .NET SIMD-enabled types include the following:
- The <xref:System.Numerics.Vector2>, <xref:System.Numerics.Vector3>, and <xref:System.Numerics.Vector4> types, which represent vectors with 2, 3, and 4 <xref:System.Single> values.
@ -85,11 +99,12 @@ The .NET SIMD-enabled types include the following:
> [!NOTE]
> The <xref:System.Numerics.Vector%601> type is included with .NET Core and .NET 5+, but not .NET Framework. If you're using .NET Framework, install the [System.Numerics.Vectors](https://www.nuget.org/packages/System.Numerics.Vectors) NuGet package to get access to this type.
The SIMD-enabled types are implemented in such a way that they can be used with non-SIMD-enabled hardware or JIT compilers. To take advantage of SIMD instructions, your 64-bit apps must be run by the runtime that uses the RyuJIT compiler, which is included in .NET Core and in .NET Framework 4.6 and later versions. It adds SIMD support when targeting 64-bit processors.
For more information, see [Use SIMD-accelerated numeric types](simd.md).
## See also
- [Standard Numeric Format Strings](base-types/standard-numeric-format-strings.md)
- [Standard numeric format strings](base-types/standard-numeric-format-strings.md)
- [Floating-point numeric types in C#)](../csharp/language-reference/builtin-types/floating-point-numeric-types.md)

Просмотреть файл

@ -1,25 +0,0 @@
using System;
public class Example
{
public static void Main()
{
// <SnippetGeneralFormatSpecifier>
double original = 0.84551240822557006;
var rSpecifier = original.ToString("R");
var g17Specifier = original.ToString("G17");
var rValue = Double.Parse(rSpecifier);
var g17Value = Double.Parse(g17Specifier);
Console.WriteLine($"{original:G17} = {rSpecifier} (R): {original.Equals(rValue)}");
Console.WriteLine($"{original:G17} = {g17Specifier} (G17): {original.Equals(g17Value)}");
// The example displays the following output:
// 0.84551240822557006 = 0.84551240822557: False
// 0.84551240822557006 = 0.84551240822557006: True
// </SnippetGeneralFormatSpecifier>
}
}
// The example displays the following output:
// 0.84551240822557006 = 0.84551240822557: False
// 0.84551240822557006 = 0.84551240822557006: True

Просмотреть файл

@ -1,16 +0,0 @@
Module Example
Public Sub Main()
Dim original As Double = 0.84551240822557006
Dim rSpecifier = original.ToString("R")
Dim g17Specifier = original.ToString("G17")
Dim rValue = Double.Parse(rSpecifier)
Dim g17Value = Double.Parse(g17Specifier)
Console.WriteLine($"{original:G17} = {rSpecifier} (R): {original.Equals(rValue)}")
Console.WriteLine($"{original:G17} = {g17Specifier} (G17): {original.Equals(g17Value)}")
End Sub
End Module
' The example displays the following output:
' 0.84551240822557006 = 0.84551240822557 (R): False
' 0.84551240822557006 = 0.84551240822557006 (G17): True