GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Type Conversion
Selected Reading
C# - Type ConversionType conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms − Implicit Type ConversionThe implicit conversions are performed by the C# compiler in a type-safe manner. For example, a value can be converted from one data type to another without requiring explicit casting, from smaller to larger integral types, or from derived classes to base classes. ExampleIn this example, we demonstrate implicit casting, which occurs automatically when a smaller size type passed to a larger size type − using System;
namespace MyExample {
class Example {
static void Main(string[] args) {
int myInt = 9;
// Automatic casting: int to double
double myDouble = myInt;
Console.WriteLine(myInt);
Console.WriteLine(myDouble);
}
}
}
When the above code is compiled and executed, it produces the following result − 9 9 Explicit Type ConversionThe explicit conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator. Example 1The following example shows an explicit type conversion, Here we convert double to int − using System;
namespace TypeConversionApplication {
class ExplicitConversion {
static void Main(string[] args) {
double d = 5673.74;
int i;
// cast double to int.
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result − 5673 Example 2In the following example, we manually cast an int to a float in C# using explicit type conversion: using System;
namespace MyApplication {
class Program {
static void Main(string[] args) {
int myInt = 10;
// Manual casting: int to float
float myFloat = (float) myInt;
Console.WriteLine(myInt);
Console.WriteLine(myFloat);
}
}
}
When the above code is compiled and executed, it produces the following result − 10 10 Type Conversion Using Convert ClassThe Exampleusing System;
namespace ConversionExample {
class Program {
static void Main(string[] args) {
string str = "123";
int num = Convert.ToInt32(str);
Console.WriteLine(num);
}
}
}
When the above code is compiled and executed, it produces the following result − 123 Type Conversion Using Parse() MethodThe Exampleusing System;
namespace ParseExample {
class Program {
static void Main(string[] args) {
string str = "456";
int num = int.Parse(str);
Console.WriteLine(num);
}
}
}
When the above code is compiled and executed, it produces the following result − 456 Type Conversion Using TryParse() MethodThe Exampleusing System;
namespace TryParseExample {
class Program {
static void Main(string[] args) {
string str = "789";
if (int.TryParse(str, out int result)) {
Console.WriteLine(result);
} else {
Console.WriteLine("Conversion failed.");
}
}
}
}
When the above code is compiled and executed, it produces the following result − 789 C# Type Conversion MethodsC# provides the following built-in type conversion methods −
ExampleThe following example converts various value types to string type − using System;
namespace TypeConversionApplication {
class StringConversion {
static void Main(string[] args) {
int i = 75;
float f = 53.005f;
double d = 2345.7652;
bool b = true;
Console.WriteLine(i.ToString());
Console.WriteLine(f.ToString());
Console.WriteLine(d.ToString());
Console.WriteLine(b.ToString());
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result − 75 53.005 2345.7652 True Advertisements
| ||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |