Data Type and Type Conversion in C
Data Type and Type Conversion in C
NET
In this article I will explain about one of the main topics in C# - Data Types. You will
also learn about the value and reference types, type conversions and other features
related to data types.
C# is a strongly typed language; therefore every variable and object must have a
declared type.
Proper utilization of correct data types allows developers to make the most of the
language
Data type in C#
There are two types of data type in C#:
• byte
• short
• int
• float
• double
• long
• char
• bool
• datetime
• string
• object
etc
• class
• struct
• enum
• interface
• delegate
• array
Encoding Scheme To represent coding scheme.
ASCII 8 bits 28 = 256
8
ANSI 7 bits 7 = 128
16
Unicode character sets 16 bits 2 = 65000
• UTF 7
• UTF 8
• UTF 16
• UTF 32
Integer Type
C# supports eight predefined integer types,
In application context, value types are stored in stack but reference types are
stored in managed heap.
Value Type
• Value types are fixed in size.
• Value types are made in system stack.
• Actual values of data are stored in stack.
• If you assign a value of a variable to another it will create two copies.
All primitive data type except string and object are example of value types.
Object is a super type. It can store any type and any size of data. Object is called
super type because it helps in inheritance.
Note
Stack is an operation entity (LIFO) i.e. it is fixed in size.
Reference Type
• Reference types are not fixed in size.
• They are maintained in system managed heap but it also uses stack to
store reference of heap.
• Two primitive types (string and object) and non-primitive data types
(class, interface & delegate) are examples of reference type.
CLR manages heap (large memory area). Heap address is accessed from stack. In
reference type reference is used for processing using both managed heap and
stack (operational entity).
Type Conversions
Conversion is based on type compatibility and data compatibility.
1. Implicit Conversion
2. Explicit Conversion
Implicit Conversion
In implicit conversion the compiler will make conversion for us without asking.
Below table shows the implicitly type conversions that are supported by C#,
From To
sbyte short, int, long, float, double, decimal
byte short, ushort, int, uint, long, ulong, float, double, decimal
short int, long, float, double, decimal
ushort int, uint, long, ulong, float, double, decimal
int long, float, double, decimal
uint long, ulong, float, double, decimal
long float, double, decimal
ulong float, double, decimal
float double
char ushort, int, uint, long, ulong, float, double, decimal
Explicit Conversion
In explicit conversion we specifically ask the compiler to convert the value into
another data type.
Explicit conversion is carried out using casts. When we cast one type to another, we
deliberately force the compiler to make the transformation.
You should never expect that the cast would give you best or correct result. Casts
are potentially unsafe. Casting of big data type into small may lead to loosing of
data.
Practical demonstration of explicit conversion,
1. using System;
2. namespace explicit_conversion
3. {
4. class Program
5. {
6. static void Main(string[] args)
7. {
8. int num = 65;
9. char alpha;
10. alpha = (char)num;
11. // In this the int values are explicitly convert
ed to char data type;
12. //you have to tell compiler to do the conversion
, it uses casting.
13. Console.WriteLine($"alphabet is: {alpha}");
14. Console.ReadLine();
15. }
16. }
17. }
Note
You can also use Explicit Cast Operator () and unboxing for explicit conversion.
1. Parsing
2. Convert Class
3. Explicit Cast Operator ()
Parsing
Parsing is used to convert string type data to primitive value type. For this we use
parse methods with value types.
1. using System;
2. namespace parsing
3. {
4. class Program
5. {
6. static void Main(string[] args)
7. {
8. //using parsing
9. int number;
10. float weight;
11. Console.Write("Enter any number : ");
12. number = int.Parse(Console.ReadLine());
13. Console.Write("Enter your weight : ");
14. weight = float.Parse(Console.ReadLine());
15. Console.WriteLine($"You have entered : {number}"
);
16. Console.WriteLine($"You weight is : {weight}");
17. Console.ReadLine();
18. }
19. }
20. }
Convert Class
One primitive type to another primitive type.
This class contains different static methods like ToInt32(), ToInt16(), ToString(),
ToDateTime() etc used in type conversion.
1. using System;
2. namespace convert_conversion
3. {
4. class Program
5. {
6. static void Main(string[] args)
7. {
8. // example of using convert class
9. string num = "23";
10. int number = Convert.ToInt32(num);
11. int age = 24;
12. string vote = Convert.ToString(age);
13. Console.WriteLine($"Your number is : {number}");
1. using System;
2. namespace explicit_cast_conversion
3. {
4. class Program
5. {
6. static void Main(string[] args)
7. {
8. int num1, num2;
9. float avg;
10. num1 = 10;
11. num2 = 21;
12. avg = (float)(num1 + num2) / 2;
13. Console.WriteLine($"average is : {avg}");
14. Console.ReadLine();
15. }
16. }
17. }
Boxing
• Boxing is a mechanism in which value type is converted into reference
type.
• It is implicit conversion process in which object type (super type) is used.
• In this process type and value both are stored in object type
Unboxing
• Unboxing is a mechanism in which reference type is converted into
value.
• It is explicit conversion process.
Program to show boxing and unboxing:
1. using System;
2. namespace boxing
3. {
4. class Program
5. {
6. static void Main(string[] args)
7. {
8. int i = 10;
9. object o = i; // boxing
10. int j = (int)o; // unboxing
11. Console.WriteLine("value of o object : " + o);
12. Console.WriteLine("Value of j : " + j);
13. Console.ReadLine();
14. }
15. }
16. }
Hope you have got some idea about data types, value type and reference type and
boxing and unboxing. Your feedback and constructive contributions are
welcome. Please feel free to contact me for feedback or comments you may have
about this article.