WebClient Unicode - Which UTF8?

C#.NetUnicodeUtf 8Webclient

C# Problem Overview


When I create a WebClient to consume some RESTful xml, I can specify the unicode encoding 2 ways:

WebClient wc = new WebClient ();
wc.Encoding = Encoding.UTF8;
wc.Encoding = UTF8Encoding.UTF8;

Which is correct/better ?

C# Solutions


Solution 1 - C#

They're identical.

UTF8Encoding inherits Encoding.
Therefore, you can access all of the static members declared by Encoding through the UTF8Encoding qualifier.

In fact, you can even write ASCIIEncoding.UTF8, and it will still work.

It will compile to identical IL, even in debug mode.


I would recommend using Encoding.UTF8, as it shows what's going on more clearly.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIan VinkView Question on Stackoverflow
Solution 1 - C#SLaksView Answer on Stackoverflow