Share via


.NET: Declaring Numeric Data Types

Overview / Survival Guide

The other day I was thinking about the many forum examples(including ones that I have provided) where it would appear that memory consumption may have not been taken into consideration. In .NET (Visual Basic & C#), there are several numeric data types out there for you to use, but which one should you choose? This article is meant to help you select a numeric data type that best fills your needs, while also balancing outperformance with possible data storage concerns.

Binary Data

In computer science, it is a well-known fact that the easiest way for a computer to process data is using a base 2 numbering system known as the binary numbering system. The reason that the binary numbering system is the easiest way for a computer to process base two numbers is that ones and zeros can be represented with either a high or a low voltage state. For example, a one can be represented typically with 5 volts, whereas zero is represented with 0 volts. The image below shows how one might represent the number 85 with a binary signal.

Since humans have 10 fingers, it makes the most sense for us to count to 10(use all our fingers), before carrying a digit to the next place. In electronics, it is most natural for electricity to have 2 states: On, or Off. We can call these two states whatever we want:(On/Off, High/Low, or 1/0). In base 10 when we get to 9, if we add one number, it will carry from the one's column to the 10's column, and when we reach 99 and add one to that, it will carry to the 100's column, and so on and so forth. Well with two bases, when we have the number 1 and add 1 to it, then automatically, it will carry over to the twos column. When we have a one in the twos column and a one in the ones column, then if we add one, that will carry all the way over to 4's column. Below is an image that shows how the number columns of the same number(85) apply.

Data Sizes

One might be able to gain some insight on to how valuable paying attention to data sizes might be in the long run by taking a look at a visual representation of data sizes. In the below image, you can see how larger data types can quickly consume more resources than smaller datatypes. In iteration, this can potentially consume great quantities of storage space.

A rough guideline for data type selection...

What are you using the numeric data type for?

Will this variable require the use of fractional numbers (non-whole rational numbers)?

No fractional numbers

Which range will this variable stay within?

  • If 0 to 255 then choose  Byte(8 Bit)
  • If 0 to 65535 then choose ** UInt16**/**UShort **(2 Bytes/16 Bit)
  • If 0 to 4294967295 then choose : UInt32/**UInteger **(4 Bytes/32 Bit)
  • If 0 to 18446744073709551615 then choose  UInt64/**ULong **(8 Bytes/64 Bit)
  • If -32768 to 32767 then choose  Int16/**Short **(2 Bytes/16 Bit)
  • If -2147483648 to 2147483647 then choose  Int32/**Integer **(4 Bytes/32 Bit)
  • If -9223372036854775808 to 9223372036854775807 then choose  Int64/**Long **(8 Bytes/64 Bit)

With fractional numbers

What's more important?

  • If Memory Usage then choose  **Single **(4 Bytes/32 bit)
  • If Accuracy(twice as precise as a single) & Memory Usage then choose  Double (8 Bytes/64 bit)
  • If You Require Absolute Accuracy, then choose  Decimal (16 bytes/128 bit)

Summary

At declaration, taking extra consideration into selecting a numeric datatype may be beneficial, and help to reduce unnecessary memory consumption.

See also

Please check out my other TechNet Wiki articles!