C# Data Types

In this lesson, we will discuss the following data types in C# with examples: int, long, float, double, bool, char, and string.

Here is the list of data types in C# with examples:

  • int
  • long
  • float
  • double
  • bool
  • char
  • string

The int datatype

The int type in C# includes whole numbers, positive or negative from -2147483648 to 2147483647. Using this, create integer variables. Declare an integer data type with the int keyword.

Let us see an example to declare and initialize an integer variable:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      // Declare and initialize an integer variable
      int i = 5;
      
      // Display the integer 
      Console.WriteLine("Value = "+i);
    }
  }
}

Output

Value = 5

The long datatype

The long type in C# includes whole numbers, positive or negative from -9223372036854775808 to 9223372036854775807. Using this, store the values bigger than int i.e. large numbers. Declare a long data type with the long keyword.

In C#, to use the long type, append the variable value with L. Let us see an example to declare and initialize a long variable:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      // Declare and initialize a long variable
      // Note, we ended the long value with L
      long i = 1288000000L;
      
      // Display the long value 
      Console.WriteLine("Value = "+i);
    }
  }
}

Output

Value = 1288000000

The float datatype

The float datatype in C# includes a number with a decimal. Use it to store fractions. Declare a float data type with the float keyword.

In C#, to use the float type, append the variable value with F. Let us see an example to declare and initialize a float variable:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      // Declare and initialize a float variable
      // Note, we ended the float value with F
      float f = 3.98F;
      
      // Display the float value 
      Console.WriteLine("Value = "+f);
    }
  }
}

Output

Value = 3.98

The double datatype

The double type in C# includes a number with a decimal. Use it to store fractions. Declare a double data type with the double keyword.

Now, you would be wondering about the difference between the float and decimal type. The double is given more preference because:

The precision of double variables is:

15 digits

However, the precision of float variables is :

six or seven decimal digits

Precision means the number of digits allowed after the decimal point.

In C#, to use the double type, append the variable value with D. Let us see an example to declare and initialize a double variable:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      // Declare and initialize a double variable
      // Note, we ended the double value with D
      double d = 12.37D;
      
      // Display the double value 
      Console.WriteLine("Value = "+d);
    }
  }
}

Output

Value = 12.37

The bool datatype

The bool type in C# is used to store Boolean values i.e. the true/ false values. Declare a boolean data type with the bool keyword.

Let us see an example to declare and initialize a bool variable:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      // Declare and initialize a bool variable
      bool b = true;
      
      // Display the boolean value 
      Console.WriteLine("Value = "+b);
    }
  }
}

Output

Value = True

The char datatype

The char type in C# is used to store a character value, for example, A, B, C, D, etc. Declare a char data type with the char keyword. The char variable value is surrounded by single quotes.

Let us see an example to declare and initialize a char variable:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      // Declare and initialize a char variable
      char c = 'K';
      
      // Display the char value 
      Console.WriteLine("Value = "+c);
    }
  }
}

Output

Value = K

The string datatype

The string type in C# is used to store a sequence of characters, for example, amit, john, sachin, etc. Declare a string data type with the string keyword. The string variable value is surrounded by double quotes.

Let us see an example to declare and initialize a string variable:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      // Declare and initialize a string variable
      string s = "Amit Diwan";
      
      // Display the string value 
      Console.WriteLine("Value = "+s);
    }
  }
}

Output

Value = Amit Diwan
C# Decision Making Statements
C# Operators
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment