简体   繁体   English

C#中是否有内置的编码枚举?

[英]Is there any built-in Encoding Enum in C#?

I have a function that receive an Encoding object as parameter. 我有一个函数,接收一个Encoding对象作为参数。 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). 但是,我认为对于其他程序员来说,传递一个枚举值而不是一个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? 我在C#中找不到任何内置的Enum枚举...我错过了某些东西还是应该创建自己的枚举?

The idea of using an enum was to help programmer that may not know where to get the different Encoding objects 使用枚举的想法是为了帮助可能不知道从何处获取不同Encoding对象的程序员。

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. 想要更改编码的开发人员将知道(或至少需要知道)他们在做什么,因此,他们应该知道如何获取代表他们要使用的编码的Encoding实例。

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() . 如果没有,他们可以在网络上搜索“ .net获取代码页XYZ的编码” ,将其定向到MSDN页面以获取Encoding.GetEncoding() Whereas if you introduce a new enum, their Google search for ".net get TheBlackSmurf.EncodingEnum for codepage XYZ" will yield zero results. 而如果您引入一个新的枚举,则他们在Google上搜索“ .net以获得代码页XYZ的TheBlackSmurf.EncodingEnum”将产生零结果。

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. 另一方面,如果出于某种原因您的软件只能支持少量编码,则枚举可能是一个可行的解决方案。

Well no, there is no enum. 好吧,没有枚举。 But there are pre-defined Encodings as static objects in the Encoding class (see https://msdn.microsoft.com/en-us/library/system.text.encoding.aspx ), like ASCII and UTF8 that basically provide the same functionality as an enum would. 但是,在Encoding类中有一些作为静态对象的预定义的Encoding(请参阅https://msdn.microsoft.com/en-us/library/system.text.encoding.aspx ),例如ASCII和UTF8基本上提供了相同的对象枚举的功能。

If you absolutely need to use an enum, you can write your own that maps to the different pre-defined encodings. 如果绝对需要使用枚举,则可以编写自己的映射到不同的预定义编码。 It's only a couple of lines of code. 这只是几行代码。

Agreeing with @MarioDS's comment, this is as far as I would go: 同意@MarioDS的评论,就我所愿:

XML Docs to feed IntelliSense XML文档来提供IntelliSense

///<param name="encoding">
///    Whatever it means to this function…. 
///    Examples: Encoding.Unicode or Encoding.GetEncoding(1252)
///</param>

(You could get fancier with see , seealso , cref or whatever but that would clutter up the source to little advantage.) (您可能会对seeseealsocref或其他东西产生更多的seealso ,但这会使源头混乱,几乎没有优势。)

In use: 正在使用:

使用IntelliSense工具提示显示函数调用的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM