Data types in C#

October9, 2017
by admin

What are data types?

Data types informs the compiler what type data we are going to store in memory in order to access it later. Basically data types are used in conjunction with variables. If you are new to programming and don’t know about what are variables then don’t worry. We are going to learn it after discussing data types. For now lets just discuss available commonly used data types in C# that you have to use in your everyday coding.


Commonly used Data types in C#:

1. ‘int’ data type:

This is commonly used data type to store whole number. We need to use ‘int’ keyword.

Ex:

int x;   // here x is a variable with data type int

Other integer data types are as follows:

byte, short, long, sbyte, uint, ushort and ulong.

All these store integer data, but has different storage capacity.

The ‘int’ is the most commonly used data types to store integer value. Its range is from -2,147,483,648 to 2,147,483,647.

If you want to store more range value, then use ‘long’ data type. It has more storage capacity than ‘int’ data type.

If you want to store very small value and you are sure it will not exceed in the flow of the application then you can use ‘short’ data type. Its range is from -32,768 to 32,767.

The ‘sbyte’ data type has a smallest storage capacity. Its range is -128 to 127.

Data types that are prefixed with the letter ‘u’ stores unsigned type that means they store only positive values.
For example, ‘unit’ data type range is 0 to 4,294,967,295.

Every data type has a default value.

The default value of integer data types is 0.

2. ‘float’ data type:

If want to store value with fractions, then use ‘float’ data type. Use ‘float’ keyword to store ‘float’ data type value.

Ex:

float y = 24.65;

Float is a single precision floating point type. The default value of float data type is 0.0F;

If you want to store value having more capacity, then use the ‘double’ data type.

Ex:

double m;

The ‘double’ data type is a double precision floating point type. The default value of the ‘double’ data type is 0.0D;

If you still want to store value with more capacity, then use ‘decimal’ data type.

Ex:

decimal n;

The default value of ‘decimal’ data type is 0.0M;

3. ‘bool’ data type:

Use ‘bool’ keyword to use boolean data type. It stores either true or false value.

Ex:

bool isActive;

The default value of ‘bool’ data type is false.

4. ‘char’ data type:

The ‘char’ data type is used to store single character. It stores the unicode character.

Ex:

char j = 's';

The default value of ‘char’ data type is ‘\0’.

5. ‘string’ data type:

If you want to store more that one character, then use ‘string’ data type.

For example, if you want to store the name of a person or city, state or country then use ‘string’ data type.

Ex:

string name = "Chittaranjan";

The ‘string’ data type is an alias for the class ‘System.String’.

To store single character value in the ’char’ data type variable, we have to use single quotes while for ’string’ data type value we have to use double quotes.

6. ‘object’ data type:

To store any type of value use ‘object’ data type. To use ‘object’ data type use ‘object’ keyword.

Ex:

object o;
o = 10;

This data type is an alias of ‘System.Object’ class.

If you are new to programming and don’t know about class then no worry. We are going to discuss about the ‘class’ later in this course.


These are the common data types used in C# coding.

Data types in C# are classified into two categories, value types and reference types.

All data types other than ‘string’ and ‘object’ we have discussed are of value types and also known as primitive data types.

The ‘string’ and ‘object’ data types are reference types.

We are going to discuss about value types and reference types and differences between them later in this course.

But for now this is enough introduction about data types in C# so that you will be familiar with them to understand its use in our upcoming topics including the next topic on variables and constants.

C# Fundamentals