C# Data Types

The following is built-in data types supported by C#.

Type.NET Framework TypeDescriptionRangeDefault ValueExamples
boolSystem.BooleanBoolean valuetrue or falsefalsebool aa = true;
sbyteSystem.SByte8-bit signed integer type-128 .. 1270sbyte bb = -50;
byteSystem.Byte8-bit unsigned integer type0 .. 2550byte cc = 100;
shortSystem.Int1616-bit signed integer type-32,768 .. 32,7670short dd = -2000;
ushortSystem.UInt1616-bit unsigned integer type0 .. 65,5350ushort ee = 3000;
intSystem.Int3232-bit signed integer type-2,147,483,648 .. 2,147,483,6470int ff = 123;
uintSystem.UInt3232-bit unsigned integer type0 .. 4,294,967,2950uint gg = 123456;
longSystem.Int6464-bit signed integer type-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807 0Llong hh = -9999;
ulongSystem.UInt6464-bit unsigned integer type0 .. 18,446,744,073,709,551,615 0ulong ii = 987654321;
floatSystem.Single32-bit single-precision floating type-3.402823e38 .. 3.402823e38 0.0Ffloat jj = 123.33;
doubleSystem.Double64-bit double-precision floating type-1.79769313486232e308 .. 1.79769313486232e308 0.0Ddouble kk = 55.3367;
decimalSystem.Decimal128-bit precise decimal type-79228162514264337593543950335 .. 79228162514264337593543950335 0.0Mdecimal ll = 7.112233;
charSystem.Char16-bit Unicode characterU0000 to Uffff'\0'char mm = 'Q';
stringSystem.StringUnicode charactersN/AN/Astring nn = "ert12";
objectSystem.ObjectThe base ClassN/AN/Aobject oo = new Object();

In C#, the data types are aliases of predefined types in the System namespace.

int x = 123;
System.Int32 x = 123;

The above 2 lines of code are the same. Both of them define a integer parameter with initial value 123.

  • All the built-in types except string and object are value types.
  • Object and string are reference type.
  • Value type data is stored on the stack in memory and is discarded from the stack when the method is finished running.
  • Reference type data is stored in the heap in memory and is discarded by C#'s garbage collection system if no pointer points to the data.

Example: 01-04-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;

namespace ValueTypesVSReferenceTypes
{
    class Person
    {
        public byte age;
    }

    class Program
    {
        /* Value type VS reference type
         * Author: Steven
         * Date:   2014-07-15
         */
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;
            b = a;          // a => b
            b = 3;          // 3 => b

            Person john = new Person();
            john.age = 10;
            Person mike = new Person();
            mike.age = 20;
            mike = john;    // mike and john point to the same object
            mike.age = 30;

            Console.WriteLine("a={0}, b={1}", a, b);
            Console.WriteLine("john.age={0}, mike.age={1}", john.age, mike.age);
            Console.Read();
        }
    }
}

Output

a=1, b=3
john.age=30, mike.age=30
  • Line 5-8: Define a class Person.
  • Line 12-15: C# comments. We'll explain it in the next section.
  • Line 18: Declare integer variable a and initial value 1.
  • Line 19: Declare integer variable b and initial value 2.
  • Line 20: Assign a's value to b. Now b is 1.
  • Line 21: Assign 3 to b. Now b is 3.
  • Line 23: Create an instance of Person and assign to john.
  • Line 24: Assign 10 to john's age.
  • Line 25: Create an instance of Person and assign to mike.
  • Line 26: Assign 20 to mike's age.
  • By now, the memory looks like the following.
  • Stack Heap Example 1

  • The value of john or mike is the address of the object in the heap
  • Line 27: Assign john's value to mike. Then john and mike all points to the same address of the object in heap.
  • Line 28: Assign 30 to mike's age.
  • Now the memory looks like:
  • Stack Heap Example 2

  • For a and b, the value of b changing will not affect a's value.
  • For john and mike, because they all point to the same object. If any age changing will affect both.