ASCIIEncoding Constructor

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Initializes a new instance of the ASCIIEncoding class.

C#
public ASCIIEncoding();

Examples

The following example demonstrates how to create a new ASCIIEncoding instance and display the name of the encoding.

C#
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        ASCIIEncoding ascii = new ASCIIEncoding();
        String encodingName = ascii.EncodingName;
        Console.WriteLine("Encoding name: " + encodingName);
    }
}

Remarks

Ettevaatust!

The ASCIIEncoding class does not provide error detection. For security reasons, you should use the UTF8Encoding, UnicodeEncoding, or UTF32Encoding class and enable error detection.

If you choose to use ASCII encoding, this constructor may not provide the appropriate fallback behavior for your application. It uses the EncoderReplacementFallback and DecoderReplacementFallback classes to replace every character outside the range of U+0000 through U+007F with a question mark (?). Instead, you can call the Encoding.GetEncoding(Int32, EncoderFallback, DecoderFallback) or Encoding.GetEncoding(String, EncoderFallback, DecoderFallback) method and pass it EncoderExceptionFallback and DecoderExceptionFallback objects to use exception fallback.

Märkus

ASCIIEncoding supports only the Unicode character values between U+0000 and U+007F. Therefore, UTF8Encoding, UnicodeEncoding, and UTF32Encoding are better suited for globalized applications.

Applies to

Toode Versioonid
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0