Data Types in CSharp - 1
Data Types in CSharp - 1
In Datatypes topic, we will understand what are the different data types available in .NET and in
which scenario we need to use which data types.
The Datatypes in C# are basically used to store the data temporarily in the computer through a
program.
In the real world, we have different types of data like integers, floating-point, characters,
boolean, strings, etc.
To store all these different kinds of data in a program and to perform business-related
operations, we need the data types.
The keyword which gives all the above information is called the data type in C#.
purpose : A data type in C# specifies the type of data that a variable can store such as integer,
floating, boolean, character, string, etc.
The data type which stores the value directly in the memory is called the Value Data Type in C#.
The examples are int, char, boolean, and float which store numbers, alphabets, true/false, and
floating-point numbers respectively.
The definition of these data types is a struct. Struct is a value type in C#.
The value data types in C# again classified into two types are as follows.
Predefined Data Types – Example includes Integer, Boolean, Boolean, Long, Double, Float, etc.
1
See, on our computer’s hard disk, we have some data, let’s say A.
The data can be in different formats, it can be an image, it can be an alphabet, it can be digits, it
can be a PDF File, etc.
Now, we know the computer can only understand binary numbers i.e. 0’s and 1’s.
So, the letter A is represented in the computer as 8 bits i.e. 01000001 (65 is the ASCII Value of A
and hence the decimal number 65 is converted to its binary equivalent binary representation
which is 01000001).
So, the 0’s and 1’s are called bits. So, to store any data on the computer we need this 8-bit
format. And this complete 8-bit is called a Byte.
Now, as a dot net developer, it is very difficult for us to represent the data in binary format i.e.
using 0s and 1s. So, here, in C# language we can use the decimal format.
So, what we can do is, we will convert binary to decimal format, and internally the computer will
map the decimal format to byte format (binary format), and then by using the byte we can
represent the data. So, we can observe the byte representation of decimal number 65 is
01000001.
It is a .NET Data Type that is used to represent an 8-Bit unsigned integer.Unsigned means only
positive values. As it represents an 8-bit unsigned integer, so it can store 2 to the power of 8 i.e.
256 numbers.
As it stores only positive numbers, so the minimum value it can store is 0 and the maximum
value it can store is 255.
Now, if we want to store both positive and negative values, then we need to use the signed byte
data type i.e. SByte.
2
This SByte data type represents an 8-bit signed integer. Then what will be the maximum and
minimum values? Remember when a data type is signed, then it can hold both positive and
negative values.
In that case, the maximum needs to be divided by two i.e. 256/2 which is 128. So, it will store
128 positive numbers and 128 negative numbers. So, in this case, the positive numbers will be
from 0 to 127 and the negative numbers will be from -1 to -128.
using System;
namespace DataTypesDemo
class Program
byte b1 = 66;
//byte b2 = -100;
//byte b3 = 256;
Console.WriteLine($"Decimal: {b1}");
3
sbyte sb1 = 66;
Console.WriteLine($"\nDecimal: {sb1}");
Console.ReadKey();
4
}
Note: The most important point that we need to remember, if we want to represent 1-byte
unsigned integer number, then we need to use the Byte data type in C#. In other words, we can
also say that, if we want to store numbers from 0 to a maximum of 255 or the ASCII value of
these numbers, then we need to go for Byte data type .NET Framework. And if we want to store
numbers from -128 to 127 or their equivalent ASCII values then we need to use the SByte data
type.
Char DataType
Char is a 2-Byte length data type that can contain Unicode data. What is Unicode? Unicode is a
standard for character encoding and decoding for computers. We can use various Unicode
encodings formats such as UTF-8(8-bit), UTF-16(16-bit), and so on. Char, it represents a character
as a UTF-16 code unit. UTF-16 means 16-bit length which is nothing but 2-Bytes.
Again, it is a signed data type which means it can store only positive numbers. If we go to the
definition of char data type, we will see the Maximum and Minimum values as follows.
As char is 2-Byte length, so it will contain 216 numbers i.e. 65536. So, the minimum number is 0
and the maximum number is 65535.
using System;
namespace DataTypesDemo
class Program
char ch = 'B';
Console.WriteLine($"Char: {ch}");
5
Console.ReadKey();
See, using byte data type we can only represent a maximum of 256 characters or we can say
ASCII values. Byte will hold a maximum of 256 symbols/character, after 256 symbols/character, if
we want to store some extra symbols like the Hindi alphabet, Chinese alphabet, or some special
symbols which are not part of ASCII Characters, then it is not possible with the byte data type,
because we already store the maximum symbols or characters. So, char is a Unicode character
representation, it is having 2 Byte length and hence we can store the regional symbols, extra
symbols, and special characters using the char data type in C#.
So, in other words, the byte is good if we are doing ASCII representation. But if we are
developing a multilingual application, then we need to use Char Data Type. Multilingual
application means applications that support multiple languages like Hindi, Chinese, English,
Spanish, etc.
Now, why not we always use the char data type instead of byte data type because char is 2 bytes
and it can store all the symbols available in the world. Then why should we use the byte data
type? Char is basically used to represent Unicode characters. And when we read char data,
internally it does some kind of transformations. And there are some scenarios where we don’t
want to do such a kind of transformation or encoding.
Suppose we have a raw image file. The raw image file has nothing to do with those
transformations. In scenarios like this, we can use the Byte data type. There is something called a
byte array we can use in situations like this.
So, the byte data type is good if we are reading the raw data or binary data, or the data without
doing any kind of transformations or encoding. And char data type is good when we want to
represent or show multilingual data or Unicode data to the end user.
A char data type is storing a single character in it. if we try to add multiple characters to a char
data type, then we will get a compile time error.
6
Here we get the error if Too many characters in a character literal. This means we cannot store
multiple characters in the character literal. If we want to store multiple characters, then we need
to use the string data type in C#
A string is nothing but a series of char data types. How to know the size of a string?We need to
know the length of the string i.e. how many characters are there and then we need to multiply
the length with the size of the char data type as a String is nothing but a series of char data types.
using System;
namespace DataTypesDemo
class Program
Console.ReadKey();
7
In C#, the string is a reference type data type.if we go to the definition of string data type, then
we will see that the type is going to be a class and the class is nothing but a reference type in C#.
Byte, SByte, Char, and String data types, which are going to store textual data.
Means: Using Byte and SByte we can store ASCII characters (Decimal Value of ASCII Characters).
Using Char data type, we can store Unicode Characters (Decimal Value of Unicode Characters),
and using string we can store textual data.
How to store numeric data only. we have two types of numeric data. One with the number with
a decimal point and another with a number without the decimal point.
if we are using a short data type, it means it is Int16 i.e. 16-Bit Signed Numeric. we can use Int16
or short in our code and both are going to be the same. Similarly, if we are using int data type it
means we are using Int32 i.e. 32-Bit Signed Numeric. So, we can use Int32 or int code and both
are going to be the same. And finally, if we are using long, it means we are using 64-Bit Signed
Numeric. So, we can use Int64 or long in our code which is going to be the same.
.NET Framework provided three kinds of data types. They are as follows:
These data types are signed data types, so they can store both positive and negative numbers.
Based on the data type, the size they can hold is going to vary.
16-Bit, so it is going to store 2 to the power of 16 numbers i.e. 65536. As it is signed, so it is going
to store both positive and negative values. So, we need to divide 65536/2 i.e. 32,768. So, it is
going to store 32,768 positive numbers as well as 32,768 negative numbers.
So, the positive numbers will start from 0 up to 32,767 and the negative numbers will start from -
1 up to -32,768. So, the minimum value this data type can hold is -32,768 and the maximum
value this data type can hold is 32,767.
8
32-Bit, so it is going to store 2 to the power of 32 numbers i.e. 4,294,967,296. As it is signed, so it
is going to store both positive and negative values.
So, we need to divide 4,294,967,296/2 i.e. 2,14,74,83,648. So, it is going to store 2,14,74,83,648
positive numbers as well as 2,14,74,83,648 negative numbers.
So, the positive numbers will start from 0 up to 2,14,74,83,647 and the negative numbers will
start from -1 up to -2,14,74,83,648. So, the minimum value this data type can hold is -
2,14,74,83,648 and the maximum value this data type can hold is 2,14,74,83,647.64-Bit Signed
Numeric (Int64)
Note: If we want to know the Max value and Min value of the Numeric data type, then we need
to use MaxValue and MinValue field constants. If we want to know the size of the data type in
bytes, then we can use sizeof function and to this function, we need to pass the data type (value
type data type, not the reference type data type).
using System;
namespace DataTypesDemo
class Program
9
Console.WriteLine($"Int32 Min Value:{Int32.MinValue} and Max Value:
{Int32.MaxValue}");
Console.ReadKey();
Now, what if we want to store only positive numbers, the .NET Framework also provided the
unsigned versions of each of these data types. For example, for Int16 there is UInt16, for Int32
there is UInt32, and for Int64, there is UInt64. Similarly, for short we have ushort, for int we have
uint and for long we have ulong. These unsigned data types are going to store only positiv values.
The size of these unsigned data types is going to be the same as their signed data type.
using System;
namespace DataTypesDemo
class Program
10
short num1 = 123;
Console.ReadKey();
the min value of all these unsigned data types is 0 which means they are going to store only
positive numbers without the decimal point. we can see, that when we use the unsigned data
type, there is no division by 2 which is in the case of signed numeric data type.
11