Utilizor
Contact Us

C# Data Types

Common data types in C#.

C# Data Types

As explained in the variables chapter, a variable in C# must be a specified data type:

int myNum = 5;               // Integer (whole number)
double myDoubleNum = 5.99D;  // Floating point number
char myLetter = 'D';         // Character
bool myBool = true;          // Boolean
string myText = "Hello";     // String

Numbers

Number types are divided into two groups:

  • Integer types stores whole numbers, positive or negative (without decimals), such as 123 or -456. Types include int and long.
  • Floating point types represents numbers with a fractional part, containing one or more decimals. Types include float and double.

Boolean

A boolean data type is declared with the bool keyword and can only take the values true or false.

Examples

Integer Types

Using int and long (note the 'L' suffix for long).

using System;

class Program
{
  static void Main(string[] args)
  {
    int myInt = 100000;
    long myLong = 15000000000L;
    Console.WriteLine(myInt);
    Console.WriteLine(myLong);
  }
}

Floating Point Types

Using float (F suffix) and double (D suffix).

using System;

class Program
{
  static void Main(string[] args)
  {
    float myFloat = 5.75F;
    double myDouble = 19.99D;
    Console.WriteLine(myFloat);
    Console.WriteLine(myDouble);
  }
}

Scientific Numbers

Scientific notation with 'e'.

using System;

class Program
{
  static void Main(string[] args)
  {
    float f1 = 35e3F;
    double d1 = 12E4D;
    Console.WriteLine(f1);
    Console.WriteLine(d1);
  }
}

Booleans

True and false values.

using System;

class Program
{
  static void Main(string[] args)
  {
    bool isCSharpFun = true;
    bool isFishTasty = false;
    Console.WriteLine(isCSharpFun);   
    Console.WriteLine(isFishTasty);   
  }
}

Characters

Single character inside single quotes.

using System;

class Program
{
    static void Main()
    {
        char myGrade = 'B';
        Console.WriteLine(myGrade);
    }
}