简体   繁体   English

转换为int16,int32,int64 - 您如何知道选择哪一个?

[英]Converting to int16, int32, int64 - how do you know which one to choose?

I often have to convert a retreived value (usually as a string) - and then convert it to an int. 我经常需要转换一个retreived值(通常作为字符串) - 然后将其转换为int。 But in C# (.Net) you have to choose either int16, int32 or int64 - how do you know which one to choose when you don't know how big your retrieved number will be? 但是在C#(。Net)中,您必须选择int16,int32或int64 - 当您不知道检索到的数字有多大时,您如何知道选择哪一个?

Everyone here who has mentioned that declaring an Int16 saves ram should get a downvote. 这里所有提到声明Int16拯救公羊的人都应该得到一个downvote。

The answer to your question is to use the keyword "int" (or if you feel like it, use "Int32"). 您的问题的答案是使用关键字“int”(或者如果您喜欢它,请使用“Int32”)。

That gives you a range of up to 2.4 billion numbers... Also, 32bit processors will handle those ints better... also (and THE MOST IMPORTANT REASON ) is that if you plan on using that int for almost any reason... it will likely need to be an "int" (Int32). 这可以为您提供高达24亿的数字...此外,32位处理器将更好地处理这些内容...而且(如果您最重要的原因 )是,如果您计划在几乎任何原因使用该int ...它可能需要是一个“int”(Int32)。

In the .Net framework, 99.999% of numeric fields (that are whole numbers) are "ints" (Int32). 在.Net框架中,99.999%的数字字段(即整数)是“整数”(Int32)。

Example: Array.Length, Process.ID, Windows.Width, Button.Height, etc, etc, etc 1 million times. 示例:Array.Length,Process.ID,Windows.Width,Button.Height等等100万次。

EDIT: I realize that my grumpiness is going to get me down-voted... but this is the right answer. 编辑:我意识到我的脾气暴躁会让我失望......但这是正确的答案。

Just wanted to add that... I remembered that in the days of .NET 1.1 the compiler was optimized so that 'int' operations are actually faster than byte or short operations. 只是想补充一点......我记得在.NET 1.1时代,编译器已经过优化,因此'int'操作实际上比字节或短操作更快。

I believe it still holds today, but I'm running some tests now. 我相信它今天仍然存在,但我现在正在进行一些测试。


EDIT: I have got a surprise discovery: the add, subtract and multiply operations for short(s) actually return int! 编辑:我有一个惊喜发现:short(s)的加,减和乘法运算实际上返回int!

Repeatedly trying TryParse() doesn't make sense, you have a field already declared. 反复尝试TryParse()没有意义,你已经声明了一个字段。 You can't change your mind unless you make that field of type Object. 除非你创建Object类型的字段,否则你无法改变主意。 Not a good idea. 不是个好主意。

Whatever data the field represents has a physical meaning. 无论该领域代表什么数据都具有物理意义。 It's an age, a size, a count, etc. Physical quantities have realistic restraints on their range. 这是一个年龄,大小,数量等。物理量对其范围有实际限制。 Pick the int type that can store that range. 选择可以存储该范围的int类型。 Don't try to fix an overflow, it would be a bug. 不要试图修复溢出,这将是一个错误。

Contrary to the current most popular answer, shorter integers (like Int16 and SByte) do often times take up less space in memory than larger integers (like Int32 and Int64). 与当前最流行的答案相反,较短的整数(如Int16和SByte)在内存中占用的空间通常比较大的整数(如Int32和Int64)占用的空间少。 You can easily verify this by instantiating large arrays of sbyte/short/int/long and using perfmon to measure managed heap sizes. 您可以通过实例化大型sbyte / short / int / long数组并使用perfmon来测量托管堆大小来轻松验证这一点。 It is true that many CLR flavors will widen these integers for CPU-specific optimizations when doing arithmetic on them and such, but when stored as part of an object, they take up only as much memory as is necessary. 确实,当对它们进行算术运算时,许多CLR风格会扩展这些整数以用于CPU特定的优化,但是当作为对象的一部分存储时,它们只占用所需的内存。

So, you definitely should take size into consideration especially if you'll be working with large list of integers (or with large list of objects containing integer fields). 所以,你肯定应该考虑大小,特别是如果你将使用大型整数列表(或包含整数字段的大型对象列表)。 You should also consider things like CLS-compliance (which disallows any unsigned integers in public members). 您还应该考虑像CLS一样的事情(不允许公共成员中的任何无符号整数)。

For simple cases like converting a string to an integer, I agree an Int32 (C# int) usually makes the most sense and is likely what other programmers will expect. 对于像将字符串转换为整数这样的简单情况,我同意Int32(C#int)通常最有意义,并且很可能是其他程序员所期望的。

If we're just talking about a couple numbers, choosing the largest won't make a noticeable difference in your overall ram usage and will just work. 如果我们只是谈论几个数字,那么选择最大的数字不会对你的整体ram使用产生明显的影响而且会起作用。 If you are talking about lots of numbers, you'll need to use TryParse() on them and figure out the smallest int type, to save ram. 如果你在谈论很多数字,你需要在它们上面使用TryParse()并找出最小的int类型,以节省ram。

All computers are finite. 所有电脑都是有限的。 You need to define an upper limit based on what you think your users requirements will be. 您需要根据您认为的用户要求定义上限。

If you really have no upper limit and want to allow 'unlimited' values, try adding the .Net Java runtime libraries to your project, which will allow you to use the java.math.BigInteger class - which does math on nearly-unlimited size integer. 如果你真的没有上限并希望允许“无限”值,请尝试将.Net Java运行时库添加到项目中,这将允许您使用java.math.BigInteger类 - 它几乎无限大小的数学运算整数。

Note: The .Net Java libraries come with full DevStudio, but I don't think they come with Express. 注意:.Net Java库带有完整的DevStudio,但我不认为它们带有Express。

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

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