在c#中,int和Int32是同一个东西,但我读过很多次int比Int32更受欢迎,没有给出原因。这是有原因的吗,我应该在意吗?
当前回答
当您只需要处理一种语言时,类型的字节大小就不太有趣了(对于那些不需要提醒自己数学溢出的代码)。有趣的部分是当你在一种语言和另一种语言之间建立桥梁,c#到COM对象,等等,或者你正在做一些位转移或屏蔽,你需要提醒自己(和你的代码审查同事)数据的大小。
在实践中,我通常使用Int32来提醒自己它们的大小,因为我确实写托管c++(例如桥接到c#)以及非托管/本机c++。
你可能知道,在c#中是64位,但在原生c++中,它最终是32位,或者char是unicode/16位,而在c++中是8位。但是我们是怎么知道的呢?答案是,因为我们已经在手册上查过了,上面是这么说的。
随着时间的推移和经验的积累,当你在c#和其他语言之间编写代码时,你会开始更加注重类型(这里的一些读者会想“为什么你会这样做?”),但恕我直言,我认为这是一个更好的实践,因为我不记得我上周写了什么(或者我不必在我的API文档中指定“此参数是32位整数”)。
在f#中(尽管我从未使用过它),它们定义了int, int32和nativeint。同样的问题也会出现,“我该用哪一个?”正如其他人所提到的,在大多数情况下,它不应该是重要的(应该是透明的)。但我个人会选择int32和uint32来消除歧义。
我想这取决于你在编写什么应用程序,谁在使用它,你和你的团队遵循什么编码实践,等等,来证明什么时候使用Int32。
附录: 顺便说一句,自从我几年前回答了这个问题之后,我就开始同时使用f#和Rust了。f#,它都是关于类型推断,以及c#和f#之间的桥接/互操作,本机类型匹配,所以不用担心;我很少需要在f#中显式地定义类型(如果不使用类型推断,这几乎是一种罪过)。在Rust中,他们完全消除了这种歧义,你必须使用i32 vs u32;总而言之,减少歧义有助于减少bug。
其他回答
虽然它们(大部分)是相同的(参见下面的一个[错误]差异),但您绝对应该注意并应该使用Int32。
The name for a 16-bit integer is Int16. For a 64 bit integer it's Int64, and for a 32-bit integer the intuitive choice is: int or Int32? The question of the size of a variable of type Int16, Int32, or Int64 is self-referencing, but the question of the size of a variable of type int is a perfectly valid question and questions, no matter how trivial, are distracting, lead to confusion, waste time, hinder discussion, etc. (the fact this question exists proves the point). Using Int32 promotes that the developer is conscious of their choice of type. How big is an int again? Oh yeah, 32. The likelihood that the size of the type will actually be considered is greater when the size is included in the name. Using Int32 also promotes knowledge of the other choices. When people aren't forced to at least recognize there are alternatives it become far too easy for int to become "THE integer type". The class within the framework intended to interact with 32-bit integers is named Int32. Once again, which is: more intuitive, less confusing, lacks an (unnecessary) translation (not a translation in the system, but in the mind of the developer), etc. int lMax = Int32.MaxValue or Int32 lMax = Int32.MaxValue? int isn't a keyword in all .NET languages. Although there are arguments why it's not likely to ever change, int may not always be an Int32.
缺点是要多输入两个字符和[bug]。
这不会编译
public enum MyEnum : Int32
{
AEnum = 0
}
但这将:
public enum MyEnum : int
{
AEnum = 0
}
前一段时间,我在微软做一个项目,当时微软。net CLR产品团队的人来拜访我们。这个人编写了例子,当他定义变量时,他使用“Int32”与“int”和“String”与“String”。
我记得在微软的其他示例代码中见过这种样式。所以,我做了一些研究,发现每个人都说“Int32”和“int”之间没有区别,除了语法着色。事实上,我发现很多材料都建议您使用“Int32”使您的代码更具可读性。所以,我采用了这种风格。
前几天我确实发现了不同!编译器不允许你使用" Int32 "输入enum,但当你使用" int "时,它允许你输入enum。别问我为什么,因为我还不知道。
例子:
public enum MyEnum : Int32
{
AEnum = 0
}
这个作品。
public enum MyEnum : int
{
AEnum = 0
}
取自:Int32符号vs. int
我总是在定义变量时使用别名类型(int, string等),在访问静态方法时使用真实名称:
int x, y;
...
String.Format ("{0}x{1}", x, y);
看到像int.TryParse()这样的东西似乎很难看。除了风格,我这么做没有别的原因。
这在实践中没有什么区别,最终你会采用你自己的惯例。我倾向于在分配类型时使用关键字,在使用静态方法时使用类版本等:
int total = Int32.Parse("1009");
int和Int32之间没有区别,但由于int是一个语言关键字,许多人在风格上更喜欢它(就像string vs string)。
推荐文章
- 如何强制LINQ Sum()返回0而源集合是空的
- 将值附加到查询字符串
- Selenium c# WebDriver:等待元素出现
- 我如何添加双引号的字符串,是在一个变量?
- 检查字符串是否包含字符串列表中的元素
- 最好的方法在asp.net强制https为整个网站?
- 将字符串转换为System.IO.Stream
- 我如何检查如果一个变量是JavaScript字符串?
- 如何从枚举中选择一个随机值?
- 驻留在App_Code中的类不可访问
- 在链式LINQ扩展方法调用中等价于'let'关键字的代码
- dynamic (c# 4)和var之间的区别是什么?
- Visual Studio: ContextSwitchDeadlock
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头