This is a premium alert message you can set from Layout! Get Now!

Type casting

0

C# Type Casting/ Type Conversion


What is Type-Casting?

Typecasting refers to converting the data type of a value to another data type. It can be implicitly or explicitly. In implicit typecasting, we do not need to do anything. The compiler converts data type automatically. But In Explicit typecasting, we need to convert manually.

Implicit typecasting (automatically): When we want to convert a smaller size data type to a larger size data type. Example: int (32-bit) to double (64-bit).

Explicit typecasting (manually): When we want to convert a larger size data type to a smaller size data type. Example: long (64-bit) to int (32-bit).

Why do we need Type-Casting?

Suppose, you want to add two numbers. The numbers will come from user input. Let's write C# code for that.

Type Casting

You can see that, Console.ReadLine() displaying a compile-time error. Why are we facing this error? because of Console.ReadLine() take input in string format but we store user input in an integer-type variable. A string can not convert into an int type implicitly, that's why we watching this error.

How to resolve this problem? Yes, type-casting easily solves this problem. 

Type Casting

Here Int32 represents int (Int64 represents long) type and Convert.ToInt32() converting string type to int type. It's just one way. There are many more ways. Which are displayed below.

Implicit Type Casting


int (32-bit) to long (64-bit)

          
int number1 = 100;
long number2 = number1;

int (32-bit) to float (32-bit)

          
int number1 = 100;
float number2 = number1;

int (32-bit) to double (64-bit)

           
int number1 = 100;
double number2 = number1;

char (16-bit) to int (32-bit)


char ch = 'A';
int number = ch;
Console.WriteLine(number);
//Output: 65
//because ASCII code of 'A' is 65 which is integer number.

Explicit Type Casting

1. Using Type Name

long (64-bit) to int (32-bit)


long number1 = 9875987679;
int number2 = (int)number1;

float (32-bit) to int (32-bit)

float number1 = 98.6F;
int number2 = (int)number1;

 double (64-bit) to int (32-bit)

double number1 = 23.69D;
int number2 = (int)number1;

2. String to others type

string to byte (8-bit)

string code = "123";
byte stringToByte = Convert.ToByte(code);

string to short (16-bit)

string code = "123";
short stringToShort = Convert.ToInt16(code);

string to int (32-bit)

string code = "123";
int stringToInt = Convert.ToInt32(code);

string to float (32-bit)

string code = "123";
float stringToFloat = Convert.ToSingle(code);

string to double (64-bit)

string code = "123";
double stringToDouble = Convert.ToDouble(code);

string to long (64-bit)

string code = "123";
long stringToLong = Convert.ToInt64(code);

string to Decimal

string code = "123";
decimal stringToDecimal = Convert.ToDecimal(code);

Using Parse

string number = "456";
int stringToInt = Int32.Parse(number);
//Similar code for others

3. Others to String


int number = 100;
string intToString = number.ToString();

Tags

Post a Comment

0 Comments
Post a Comment

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!
To Top