为什么我应该在 C# 中使用 int 而不是 byte 或 short [英] Why should I use int instead of a byte or short in C#

查看:29
本文介绍了为什么我应该在 C# 中使用 int 而不是 byte 或 short的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些关于这个问题的线索.大多数人似乎都喜欢在他们的 c# 代码中使用 int,即使 byte 或 smallint 会处理数据,除非它是移动应用程序.我不明白为什么.将 C# 数据类型定义为数据存储解决方案中的相同数据类型不是更有意义吗?

我的前提:如果我使用类型化数据集、Linq2SQL 类、POCO,如果我不让我的数据类型在我的层之间保持同步,我会以一种或另一种方式遇到编译器数据类型转换问题.我真的不喜欢一直做 System.Convert 只是因为在 c# 代码中更容易使用 int .我总是使用任何需要的最小数据类型来处理数据库和代码中的数据,以保持我的数据库接口干净.所以我敢打赌,我的 C# 代码中有 75% 使用的是 byte 或 short,而不是 int,因为那是数据库中的内容.

可能性:这是否意味着大多数只对代码中的所有内容都使用 int 的人也将 int 数据类型用于他们的 sql 存储数据类型,而不太关心他们数据库的整体大小,或者他们是否在适用的情况下在代码中执行 system.convert ?

我关心的原因:我一直独立工作,我只想熟悉最佳实践和标准编码约定.

解决方案

在性能方面,int 几乎在所有情况下都更快.CPU 旨在有效处理 32 位值.

较短的值处理起来很复杂.例如,要读取单个字节,CPU 必须读取包含它的 32 位块,然后屏蔽掉高 24 位.

要写入一个字节,它必须读取目标 32 位块,用所需的字节值覆盖低 8 位,然后再次写回整个 32 位块.

当然,在空间方面,您可以通过使用较小的数据类型来节省一些字节.因此,如果您正在构建一个包含几百万行的表,那么较短的数据类型可能值得考虑.(这可能也是您应该在数据库中使用较小数据类型的好理由)

在正确性方面,int 不会轻易溢出.如果您认为您的值将适合一个字节,然后在未来的某个时刻对代码进行一些看似无害的更改意味着将更大的值存储到其中怎么办?

这些是为什么 int 应该成为所有整数数据的默认数据类型的一些原因.如果您确实要存储机器字节,则仅使用字节.仅当您处理实际指定 16 位整数值的文件格式或协议或类似格式时才使用 shorts.如果您只是处理一般的整数,请将它们设为整数.

I have found a few threads in regards to this issue. Most people appear to favor using int in their c# code accross the board even if a byte or smallint would handle the data unless it is a mobile app. I don't understand why. Doesn't it make more sense to define your C# datatype as the same datatype that would be in your data storage solution?

My Premise: If I am using a typed dataset, Linq2SQL classes, POCO, one way or another I will run into compiler datatype conversion issues if I don't keep my datatypes in sync across my tiers. I don't really like doing System.Convert all the time just because it was easier to use int accross the board in c# code. I have always used whatever the smallest datatype is needed to handle the data in the database as well as in code, to keep my interface to the database clean. So I would bet 75% of my C# code is using byte or short as opposed to int, because that is what is in the database.

Possibilities: Does this mean that most people who just use int for everything in code also use the int datatype for their sql storage datatypes and could care less about the overall size of their database, or do they do system.convert in code wherever applicable?

Why I care: I have worked on my own forever and I just want to be familiar with best practices and standard coding conventions.

解决方案

Performance-wise, an int is faster in almost all cases. The CPU is designed to work efficiently with 32-bit values.

Shorter values are complicated to deal with. To read a single byte, say, the CPU has to read the 32-bit block that contains it, and then mask out the upper 24 bits.

To write a byte, it has to read the destination 32-bit block, overwrite the lower 8 bits with the desired byte value, and write the entire 32-bit block back again.

Space-wise, of course, you save a few bytes by using smaller datatypes. So if you're building a table with a few million rows, then shorter datatypes may be worth considering. (And the same might be good reason why you should use smaller datatypes in your database)

And correctness-wise, an int doesn't overflow easily. What if you think your value is going to fit within a byte, and then at some point in the future some harmless-looking change to the code means larger values get stored into it?

Those are some of the reasons why int should be your default datatype for all integral data. Only use byte if you actually want to store machine bytes. Only use shorts if you're dealing with a file format or protocol or similar that actually specifies 16-bit integer values. If you're just dealing with integers in general, make them ints.

这篇关于为什么我应该在 C# 中使用 int 而不是 byte 或 short的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆