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:

Output

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:

Output

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:

Output

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:

However, the precision of float variables is :

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:

Output

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:

Output

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:

Output

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:

Output

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

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading