C Sharp data types are the types of data assigned to the variable. You have to first specify the type of the variable in c sharp to assign an appropriate value to it. After that, you can manipulate the data with different operations.
In this tutorial, you will learn different data types for the variable with examples. So, let’s start the tutorial.
The simple example for different data types in C# is given below. The example contains an integer, float, character, string, and boolean.
1 2 3 4 5 |
int myNum = 30; float myFl = 3.2F; char myChar = 'A'; string myStr = 'This is a string.'; bool myBl = true; |
Let’s find out the C Sharp data types in detail with its memory size and range or limit to assign the value to the variable in C#.
C Sharp Types | Memory Size | Range | Description |
---|---|---|---|
short |
2 byte | -32,768 to 32,767 | Store whole numbers. |
int |
4 byte | -2,147,483,648 to 2,147,483,647 | Store whole numbers. |
long |
8 byte | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Store whole numbers with L at the end. |
float |
4 byte | 7 decimal digits | Store numeric values with decimals. |
double |
8 byte | 15 decimal digits | Store numeric values with decimals. |
decimal |
16 byte | 28 decimal digits | Numeric values with decimals and M at the end. |
char |
2 bytes | Single Character | Define character within single quotes(‘ ‘). |
string |
2 bytes per character | Text content | Text content within the double quotes (” “). |
bool |
1 bit | true or false | Used to declare a boolean variable to store boolean values either true or false. |
After you learn the details of the data types, let’s see some useful examples to declare the data type of variables. We will assign a value to it and print the value in the output using Console.WriteLine()
.
Integer C Sharp Data Types
Integer data types are the variables that store numeric values from negative to positive. These values do not contain decimal points.
short
short
is the C Sharp data type that can hold 2 bytes (16 bit) integer data within the range -32,768 to 32,767. You can use this data type to store short data of this range.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { short myNum = 30; Console.WriteLine(myNum); } } } |
Output
int
int
is a data type that holds 4 byte (32 bit) data within the range -2,147,483,648 to 2,147,483,647. It is useful to hold and common to use for storing the numeric data in C#.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { int myNum = 19; Console.WriteLine(myNum); } } } |
Output
The above example shows the output of the integer data type.
long
long
is a data type that holds 8 byte (64 bit) data within the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It is useful to store long-size numeric data in programming using C#.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { long myNum = 42; Console.WriteLine(myNum); } } } |
Output
If you want to store larger data in the future, consider using long
data type when creating applications in C#.
Float C Sharp Data Types
Float data types are the numbers that hold decimal points. If you want to store floating point numbers in C#, there are many data types available and you need to choose the right one according to your requirements.
Let’s find out float data types to store floating point numbers below.
float
float
is a data type that holds 4 byte data and 7 decimal digits to store numeric decimal values. You need to put F
alphabet at the end of the numeric value to mention that its a float value.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { float myFl = 3.2F; Console.WriteLine(myFl); } } } |
Output
The above output results in the numeric float value without that F
alphabet at the end of the number.
double
double
is the float data type that holds 8 byte (64 bit) data and 15 digits after the decimal point. It is the most common float data type to use and store decimal values in C#. If you want to store decimal numbers, it is the most recommended data type to use in applications.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { double myFl = 3.2; Console.WriteLine(myFl); } } } |
Output
The above example shows the number with a decimal point with two decimal places.
decimal
decimal
is the float data type to store larger numbers with a decimal point. It can hold upto 16 bytes (128 bit) data and 28 digits after the decimal point. If you want to store larger size decimal numbers, you can use this data to create your applications.
To create a numeric value with the decimal point of decimal data type, you have to add the alphabet M
before the number as given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { decimal myFl = 3.2M; Console.WriteLine(myFl); } } } |
Output
The above example shows the output that contains the number with 1 decimal place. However, you can add larger decimal places after the decimal points in C#.
String C Sharp Data Types
String data types can be used to store alphabet or text content in C#. The data types to store string values are given below.
char
char
data type holds 2 bytes (16 bit) data with a single character within the single quotes (‘ ‘). You can store a single character that can be small letter or capital letter.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { char myChar = 'A'; Console.WriteLine(myChar); } } } |
Output
The above example shows that the alphabet is a single character that holds a capital letter alphabet.
string
string
data type holds 2 bytes (16 bit) per character and can store text content of any size. You have to place the text content within the double quotes (” “) in C#.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { string myStr = "This is a string."; Console.WriteLine(myStr); } } } |
Output
The above example shows the output with text content using the string
data type in C#.
Boolean C Sharp Data Types
Boolean data types can be used to store boolean values in a variable. The boolean values are true
and false
.
bool
bool
is the C Sharp data type to declare a variable to store boolean value either true
or false
. It can store data of size 1 bit which is of small size data.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { bool myBl = true; Console.WriteLine(myBl); } } } |
Output
Now, you know all about types of data in C# and the name of the data type that you use to store data.