odiseh
odiseh

Reputation: 26517

How to Remember Storage Size of .NET Data Types?

In .NET framework, Knowing how much each data types consumes memory is useful, but it's so forgettable. Is there any way to remember it?

If I know for example, that the int data type consumes 4 bytes, it's useful. But this kind of data are so forgettable specifically when I use other software such as SQL Server that memory consumption there might be so different from the .NET framework.

Is there any way to remember them instead of seeing documentations?

Upvotes: 1

Views: 1309

Answers (5)

Shantilal Suthar
Shantilal Suthar

Reputation: 127

This is how I remember C# integer data types:

Step 1: Group the sizes:

  • 8 bit (1 byte).

  • 16 bits (2 bytes).

  • 32 bits (4 bytes).

  • 64 bits (8 bytes).

Step 2: Then link data types with their size:

  • 8 bit (1 byte) - byte, sbyte.
  • 16 bits (2 bytes) - short, ushort.
  • 32 bits (4 bytes) - int, uint.
  • 64 bits (8 bytes) - long, ulong.

Upvotes: 1

Robert Fraser
Robert Fraser

Reputation: 10927

For primitive types "if it sounds big, it is big" :-). A "long" is bigger than a "short". A "double" is bigger than a "float" (and something that floats around would be light, i.e. fairly small). A "byte" is bigger than a "nybble". This doesn't work for a decimal, though (decimal sounds tiny, but it's actually the largest primitive type)

In fact a double sounds like it'd be double the regular size, and if you think of int as a good baseline, than a double is indeed double the size of an int.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161783

I don't know why you're finding it useful to remember these numbers. You really don't need to bother. It's not like "back in the day", when we'd have to add up the sizes of data structures to make sure it all fit within 32 kilobytes.

Be glad that we're past that point.

Upvotes: -1

Doc Brown
Doc Brown

Reputation: 20044

Knowing the size of the basic datatypes is sometimes useful when you have to operate with low-level structures (for example, if you have to exchange data with C or assembler programs, or data from specific hardware interface), or when you have to process lots of data and need an estimation about memory consumption. But, in practice, you have only to deal with a handful (less than a dozen), so what's so hard about it to remember them?

Upvotes: 1

Jay Riggs
Jay Riggs

Reputation: 53593

I don't know if something like that is worth consciencely memorizing. I'd recommend creating a cheat sheet and refer to that as needed. In time you'll memorize it, or at the ones you use most frequently.

Upvotes: 3

Related Questions