为什么我要使用INT而不是一个字节或短于C# [英] Why should I use int instead of a byte or short in C#

查看:114
本文介绍了为什么我要使用INT而不是一个字节或短于C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在关于这个问题找到了几个线程。大多数人都是使用int类型的C#code翻过主板即使一个字节或SMALLINT将处理数据,除非它是一个移动应用程序青睐。我不明白为什么。难道不是更有意义来定义你的C#的数据类型为相同的数据类型,将在您的数据存储解决方案?

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?

我的premise:
如果我使用的是强类型DataSet,LINQ2SQL类,POCO,这种或那种方式我会碰上编译器的数据类型转换的问题,如果我不把我的数据类型同步在我的层次。我真的不喜欢做System.Convert只是因为它更容易在C#code INT使用翻过董事会的所有时光。我一直使用的任何需要的最小数据类型来处理数据库中的数据以及在code,我的界面保持到数据库干净。所以,我敢打赌我的C#code的75%是用字节或相为int短,因为这是在数据库中。

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.

可能性:
这是否意味着,谁只是在code将int用于一切大多数人还使用int数据类型为他们的SQL存储的数据类型和并不十分关心他们的数据库的整体规模,或做他们做system.convert在$ C $ ç哪里适用?

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.

推荐答案

性能方面,一个int是在几乎所有情况下更快。该CPU旨在与32位值有效地工作。

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

更短的值是复杂的处理。要读取一个字节,说了,CPU需要读取包含它的32位块,然后屏蔽掉高24位。

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.

要编写一个字节,它具有读取目标的32位块,覆盖与所需的字节值低8位,并再次写入整个32位块。

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)

和正确性的角度来看,一个int不容易溢出。如果你的认为的你的价值是要适应一个字节中,然后在未来的某个时刻到code一些看似无害的变化意味着更大的值存储成吗?

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?

这些都是一些的原因INT应该是所有集成数据的默认数据类型。只有当你真正想要存储设备字节使用字节。仅使用短裤,如果你正在处理的文件格式或协议或类似的实际指定的16位整数。如果你只是应付一般的整数,让他们整数。

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.

这篇关于为什么我要使用INT而不是一个字节或短于C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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