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

Advertisement

Visual Studio + R# – Color Extension Methods differently with ReSharper

When I read code, I find it really useful to see whether a method is real, or just an extension.

To do so, enable Color identifiers in ReSharper > Options > Code Inspection > Settings. Be aware, that this sets the ReSharper default colors in the first place.

tmpF42

Then define ReSharper Extension Method Identifier in Options > Environment > Fonts and Colors to your favorite color. I like to have them in Purple.

tmpF43

Have fun!