简体   繁体   English

内置类型,何时(不)使用?

[英]Built-in types, when (not) to use?

C# and VB.NET comes with built in types that maps to the CLR types. C#和VB.NET带有映射到CLR类型的内置类型。 Examples are: int (C#) and Integer (VB) maps to System.Int32, long (C#) and Long (VB) maps to System.Int64. 例如:int(C#)和Integer(VB)映射到System.Int32,long(C#)和Long(VB)映射到System.Int64。 What are the best practices for deciding when to use built in types or not to use them (using the System.* structs/classes instead)? 决定何时使用内置类型或不使用内置类型(代替使用System。*结构/类)的最佳实践是什么?

I nearly always use the built-in aliases, such as int/short/long. 我几乎总是使用内置别名,例如int / short / long。 They are easier to read, and do not require you to import System or to type System.Int32 everywhere, etc. 它们更易于阅读,并且不需要您导入System或在任何地方键入System.Int32等。

The language clearly defines them, and gives them a specific meaning, so I do not see any harm. 语言清楚地定义了它们,并赋予了它们特定的含义,因此我认为没有任何危害。 However, this is 100% a personal choice. 但是,这是100%的个人选择。

That being said - the one place where I do explicitly use Int32, Int16, etc., is if I'm dealing with binary storage or transfer, especially to or from a custom binary format. 话虽这么说-我确实明确使用Int32,Int16等的地方是如果我要处理二进制存储或传输,特别是与自定义二进制格式之间的往来。 In this case, having the explicit bitsize of each member going into and out of the file makes the code more readable and understandable, IMO. 在这种情况下,将每个成员的显式位大小移入和移出文件会使IMO的代码更具可读性和可理解性。

The language types (eg string, int, char) are simply Aliases for the CLR types (System.String, System.Int32, System.Char). 语言类型(例如,string,int,char)只是CLR类型(System.String,System.Int32,System.Char)的别名。

They are interchangeable, there is no need to prefer one over the other. 它们是可互换的,没有必要优先选择另一个。

EDIT 编辑

The poster asked for some help in choosing between the two, very well. 张贴者要求在两者之间进行选择时有所帮助。

Personally I tend to choose the C# language types (int, string, char etc), because they involve less typing - I suppose I'm just lazy :) 我个人倾向于选择C#语言类型(int,string,char等),因为它们涉及较少的键入-我想我只是懒惰:)

我唯一会优先使用“ System.XYZ"而不是内置类型关键字的情况是,当我需要一个非常特定大小的整数类型时,并且我希望所有阅读我的代码的人都可以清楚(例如,如果所讨论的整数实际上是4个8位字段打包在一起,请使用Int32而不是int 。)

I always use the System.* types because they look more consistent between other classes - upper case first letter and the same syntax highlighting. 我一直使用System.*类型,因为它们在其他类之间看起来更加一致-大写的首字母和相同的语法突出显示。 But that's just a personal preference and just an aesthetic issue. 但这只是个人喜好,只是审美问题。

Using "int" and "Int32" (and the others) are exactly same. 使用“ int”和“ Int32”(以及其他)完全相同。 Typicaly are used the keywords (int, Integer (vb.net), bool, etc...), because it is shorter and is highlited in IDE. 通常使用关键字(int,Integer(vb.net),bool等...),因为它较短且在IDE中高亮显示。

Rather than when to use or not use the language types versus explicit BCL class names, it is more important to know whether or not the type you intend to use is CLS Compliant. 而不是何时使用语言类型与显式BCL类名,而不是何时使用语言类型,更重要的是要知道您打算使用的类型是否符合CLS。

Specifically, the unsigned integer types are not CLS compliant because there is no requirement that a language support unsigned integer math. 具体来说,无符号整数类型不符合CLS,因为不要求语言支持无符号整数数学。

Other than this wrinkle... I would recommend whichever idiom is more in keeping with your organizations code practices. 除了这种皱纹之外,我建议您使用哪种习惯用法更符合您的组织的代码惯例。 If you fully namespace your type references, then I would continue that pattern with the System.* namespace... (I would also recommend against that practice, though, as it adds reader load without attendant gain in clarity). 如果您对类型引用完全使用命名空间,那么我将继续使用System。*命名空间...(不过,我也建议您反对这种做法,因为这会增加读取器的负担,而不会带来明显的好处)。

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

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