The_Black_Smurf The_Black_Smurf - 6 days ago 7
C# Question

Is there any built-in Encoding Enum in C#?

I have a function that receive an Encoding object as parameter. However, I think it could be easier for other programmers to pass an enum value instead of a Encoding object (especially for programmers that are not use to deal with different encoding). I couldn't find any built-in Encoding enum in C#... did I miss something or should I create an enum of my own?

Answer

The idea of using an enum was to help programmer that may not know where to get the different Encoding objects

Go for the principle of least astonishment. Developers who want to alter the encoding, will know (or at least, will need to know) what they are doing, and so they ought to know how to obtain an Encoding instance representing the encoding they want to use.

If they don't, they can search the web for ".net get encoding for codepage XYZ", directing them to the MSDN page for Encoding.GetEncoding(). Whereas if you introduce a new enum, their Google search for ".net get TheBlackSmurf.EncodingEnum for codepage XYZ" will yield zero results.

Don't cater for people who don't know what they are doing.

If instead you're going to use an enumeration, chances are that the one developer who does want to change the encoding and knows which one to use and how to obtain it, can't utilize it because you didn't think of that one and didn't include it in your enum.

On the other hand, an enum might be a viable solution, if for whatever reason your software can only support a handful of encodings.

Comments