In my previous article, C#- Programming Guide | Starting Phase Learning(4), we gave a touch about the different types of data Type in C#, so, here we will learn about the C#-type conversion .
You can click on the below link to see my previous article on Data Types C#- Programming Guide | Starting Phase Learning(4)
Type conversion or Type Casting by definition is a conversion of one type of data to another type. Eg.
let’s declare a variable
int a = 9;
Now, we want to cast it as a string type.
string b = a; This is not supported in C#
So in order to store variable ‘a’ in ‘ b’ which is of string type you need to cast.
string b = a.ToString(); //
So here we cast an integer type to string type. This is called type casting. There are different ways of type casting.
It has two forms:
1. Implicit type conversion
2.Explicit type conversion
Implicit type conversion − These conversions are done automatically when passing a smaller size type to a larger size type:
short a = 9;
double b = a;
Automatic casting: short to double
Explicit type conversion − These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
static void Main(string[] args) {
short a = 9;
double b = a;
int cast1= int(a);
int cast 2= int(b);
string b = a.ToString();
Console.WriteLine(cast1); //explicit type to int
Console.WriteLine(cast2);//explicit type to int
Console.WriteLine(b);//explicit type to string
Console.ReadKey();
}
After seeing,
Console.WriteLine()
Console.Write()
which is used to (print) values.
Now we will see how to take the value from the user.
You need to call Console.ReadLine() to get user input from the keyboard
Let’s see an example.
static void Main(string[] args)
{
string Name = Console.ReadLine();
Console.WriteLine("Your loving name is: " + Name);
Console.ReadKey();
}
Console.ReadLine();
The above statement allows us to read the input given by the user. After keying in the input I.e name we are storing into a variable Name which is of string type. So whatever the value we enter/key-in that value gets stored in the Name. Now we take the stored value and then with the hell of WriteLine we display the result on the screen.
When it comes to security services in Noida, there are several reputable companies that cater to various needs. Let me…
Are you tired of dealing with tooth decay, gum problems, and expensive dental treatments? Look no further! Steel Bite Pro…
In the world of fine jewelry, oval moissanite rings have emerged as a captivating choice for those seeking elegance and…
Noida, located in the state of Uttar Pradesh, is a fast-growing hub of digital marketing agencies in India. Choosing the…
As we move into 2023, the world of motorcycles in India is set to see some exciting changes. Manufacturers are…
The Indian Premier League (IPL) is one of the most popular and exciting cricket tournaments in the world. With the…
Leave a Comment