UTF8Encoding.Default != Encoding.UTF8 (.NET C#)

Looks like UTF8Encoding.Default would return a default instance of the UTF8Encoding, right?

Well, it doesn’t – it returns the operating system’s default ANSI encoding.

The same with ASCIIEncoding.Default, UnicodeEncoding.Default, UTF32Encoding.Default and UTF7Encoding.Default!

Why? Because they all derive from System.Text.Encoding where Default is definded:

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

tmp1F4

So instead use Encoding.ASCII, Encoding.UTF8, Encoding.UTF7, Encoding.UTF32 and Encoding.Unicode to refer explicitly to an encoding.

kick it on DotNetKicks.com

Advertisements

5 thoughts on “UTF8Encoding.Default != Encoding.UTF8 (.NET C#)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s