1. Home
  2. Computing & Technology
  3. C / C++ / C#

C# Tutorial - Learn the Fundamentals of Programming

By David Bolton, About.com

2 of 10

About Integer Types

These fall into several sizes all determined by how many bytes of storage they require and whether they can hold signed numbers (i.e. negative numbers) or unsigned. there are four sizes of integers- one byte, two bytes, four bytes and eight bytes, each with a signed and unsigned version. 8 in all. These are usually described by how many bits they occupy- so the four sizes are 8, 16, 32 and 64 bits.

The four signed types are

  • 8 Bits - sbyte - range -128 to +127.
  • 16 bit - short - range -32768 to +32767.
  • 32 bits - int - range -2,147,483,648 to +2,147,483,647
  • 64 bits - long - -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
The four unsigned types are
  • 8 Bits - byte - range 0-255.
  • 16 bit - ushort - range 0 to +32767.
  • 32 bits - uint - range 0 to 4,294,967,295.
  • 64 bits - ulong - range 0 to 18,446,744,073,709,551,615
As a general rule of thumb, you'll find that int, the signed 32 bit type will be all you need for probably 90% of applications that you write especially if you are working on a 32 bit CPU or Operating System. Using a smaller size int than the machine size may not be faster, in fact it may be slower as a 32 bit CPU retrieves data in 32 bit size chunks.

Integers, no matter what size, are probably the commonest used type as they are required for looping, indexing and counting. Example 1 shows declaration and assignment of the various types. It includes timing code so you can measure how long it takes to assign variables of the appropriate type. As a general rule, use ints- smaller sizes (byte, short, sbyte and ushort) are usually slower but if space not speed is a factor then the smaller size is more appropriate.

Download Example 1

On the next page : About Example 1

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. About Integer Types

©2009 About.com, a part of The New York Times Company.

All rights reserved.