问题描述
我刚才想实现一个类,其中大量的长/计数性能等方面均 UINT
而不是 INT
。然而,在这样做时,我注意到它实际上痛苦的这样做,就像如果实际上没有人愿意做,因为
I have just tried implementing a class where numerous length/count properties, etc. are uint
instead of int
. However, while doing so I noticed that it's actually painful to do so, like as if no one actually wants to do that.
几乎是捧出来一个整数类型一切恢复几个百分点 INT
,因此需要转换。我想构建一个的StringBuffer
与缓冲区长度默认为在类中的一个字段。需要投了。
Nearly everything that hands out an integral type returns an int
, therefore requiring casts in several points. I wanted to construct a StringBuffer
with its buffer length defaulted to one of the fields in that class. Requires a cast too.
所以我不知道我是否应该只恢复到 INT
在这里。我当然不使用的整个范围反正。我只是觉得,因为我正在处理有简单的无法的是负面的(如果是,它会是一个错误),它会是一个不错的主意,实际上使用 UINT
。
So I wondered whether I should just revert to int
here. I'm certainly not using the entire range anyway. I just thought since what I'm dealing with there simply can't be negative (if it was, it'd be an error) it'd be a nice idea to actually use uint
.
PS:我看到这个问题一>这至少解释了为什么框架本身始终使用 INT
但即使是在自己的code,它实际上繁琐坚持 UINT
这让我觉得这显然不是真的想。
P.S.: I saw this question and this at least explains why the framework itself always uses int
but even in own code it's actually cumbersome to stick to uint
which makes me think it apparently isn't really wanted.
推荐答案
虽然严格,你应该使用 UINT
对于持有非负整数变量,你所遇到的一个原因并不总是可行的。
While strictly you should use uint
for variables that hold non-negative integer you have come across one of reasons why it's not always practicable.
在这种情况下,我不认为附带不必做铸件是值得的可读性下降。
In this case I don't think the reduction in readability that comes with having to do casts is worth it.
这篇关于我应该使用C#UINT的值不能是负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!