在 C# 中接受 long 与 Int64 的样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16152868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accepted style for long vs Int64 in C#?
提问by Kyle
I know they are the same variable type, but is there an accepted standard 'style' for whether to use longor Int64?
我知道它们是相同的变量类型,但是是否有一个公认的标准“样式”来使用long或Int64?
I would like to use the most common one.
我想使用最常见的一种。
采纳答案by Julián Urbano
All documentation from Microsoft and 99.999% (or so) of the code you'll find online will use long. See for instance the numeric datatype definition in the C# reference: http://msdn.microsoft.com/en-us/library/exx3b86w.aspx
来自 Microsoft 的所有文档以及您在网上找到的 99.999%(左右)代码都将使用long. 例如,参见 C# 参考中的数字数据类型定义:http: //msdn.microsoft.com/en-us/library/exx3b86w.aspx
In the end, this is the same issue with using stringor String. In general, lowercase names are used for value datatypes like numbers or characters, and uppercase names are used for classes. In C# Int64is the complex datatype (a structure with fields, properties and methods, see the doc here) for the longvalue datatype (see the doc here). In general, people don't use the class Int64for other than invoking methods from that class, such as Int64.Parse. So you will usually find something like this:
最后,这与使用stringor 的问题相同String。通常,小写名称用于值数据类型,如数字或字符,而大写名称用于类。在 C#Int64中,值数据类型是复杂的数据类型(具有字段、属性和方法的结构,请参阅此处的文档)long(请参阅此处的文档)。通常,人们不会将该类Int64用于调用该类中的方法以外的其他用途,例如Int64.Parse. 所以你通常会发现这样的事情:
long variable = 9.5f;
string text = "8.4";
long anotherVariable = Int64.Parse(text);
回答by phadaphunk
You will get so many different opinions for a question like this since they're the exact same thing. So it's up to you. Microsoft pretty much always uses long, intand shortin their documentation and examples. It's simply an Alias in VB.NET and C#. So I guess it's better usage to use them that way.
对于这样的问题,您会得到很多不同的意见,因为它们完全相同。所以这取决于你。Microsoft 几乎总是在他们的文档和示例中使用long,int和short。它只是 VB.NET 和 C# 中的别名。所以我想以这种方式使用它们会更好。
longinstead ofInt64intinstead ofInt32shortinstead ofInt16
long代替Int64int代替Int32short代替Int16
回答by Icemanind
I think it comes down to clarity versus compatibility. Int16, Int32and Int64are more clear than short, intand long. You know just by looking at Int16that it is 16 bits in length.
我认为这归结为清晰度与兼容性。Int16,Int32并且Int64比更清晰short,int和long。仅通过查看就Int16知道它的长度为 16 位。
回答by Josh Johnson
I think in most cases it will come down to personal preference, but if you are strictly talking "C#" without specifying the underlying platform, I'd think a bit differently. If you are developing to adhere to C# standards for portability across platforms and "future-proofing", "long" would be the best way to go as it is an actual construct in the language itself. "Int64" is an actual struct (System.Int64) in the framework.
我认为在大多数情况下这将归结为个人偏好,但如果您严格地谈论“C#”而不指定底层平台,我的想法会有所不同。如果您正在开发以遵守 C# 标准以实现跨平台可移植性和“面向未来”,“long”将是最好的方法,因为它是语言本身的实际构造。“Int64”是框架中的实际结构(System.Int64)。
回答by Martin Mulder
C# uses these keywords like long, int string, as aliases for .NET-types.
C# 使用这些关键字(如 long、int string)作为 .NET 类型的别名。
int=System.Int32long=System.Int64string=System.String
int=System.Int32long=System.Int64string=System.String
The first argument to use these keywords is that it comes with the language, so use it when writing the language.
使用这些关键字的第一个参数是它随语言一起提供,因此在编写语言时使用它。
A second argument of using these keywords is that you do not have to put using System;above your codefile.
使用这些关键字的第二个参数是您不必放在代码文件using System;上方。
Another benefit could be that someone could create a new type called Int64and put it somewhere in your namespace (I know... it would be crazy). If your older code uses the type Int64, your older code could stop functioning. If your older code was using long(an alias for System.Int64) than it would still work fine.
另一个好处可能是有人可以创建一个名为的新类型Int64并将其放在您的命名空间中的某个位置(我知道......这会很疯狂)。如果您的旧代码使用 type Int64,您的旧代码可能会停止运行。如果您的旧代码正在使用long( 的别名System.Int64),那么它仍然可以正常工作。
回答by Peter
You should use long, intand short.
您应该使用long,int和short。
The one exception I know is in a function name; look at BitConverter.ToInt64.
我知道的一个例外是函数名;看看BitConverter.ToInt64。
The reason for that is that long is not defined in CILwhile int64 is.
原因是 long 没有在CIL 中定义,而 int64 是。

