views:

801

answers:

1

Why does Encoding.ASCII != ASCIIEncoding.Default in C#?

+7  A: 

This is because ASCIIEncoding derives from Encoding, where Default is defined as:

/// <summary>  
/// Gets an encoding for the operating  
/// system's current ANSI code page.  
/// </summary>  
public static System.Text.Encoding Default
{ get; }

So ASCIIEndcoding.Default actually returns operating system’s default ANSI encoding.

Anton Gogolev
So you're actually calling Encoding.Default, there is no ASCIIEncoding.Default. Resharper (and maybe FxCop) will warn you about that.
marklam
Just to clarify further: The `Default` property is a static property that is always inherited from the `Encoding` class, no matter what encoding class you call it from.
awe