Converting
one type of data into another data type is
known as Type Conversion or Type Casting. System.Convert class provides a complete set of methods for
supported conversions.They are two types:
(i)implicit
type conversion (ii) Explicit type
conversion
- Implicit type conversion - These conversions are performed automatically by the compiler and no data will be lost. For example conversion of a smaller data type to a larger data types and conversion of derived classes to base class. This is a safe type conversion.
Example :
using System;
namespace implicit_conversion
{
class Program
{
static void Main(string[]
args)
{
int
num1 = 30000;
int
num2 = 50000;
long
sum;
// In this the int values
are implicitly converted to long data type;
//you need not to tell
compiler to do the conversion, it automatically does.
sum = num1 + num2;
Console.WriteLine("Sum is : " + sum);
Console.ReadLine();
}
}
}
Implicitly type conversionstable 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 type conversion - These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator. For example conversion of larger data type to smaller data type and conversion of base class to derived classes. In this conversion information might be lost or conversion might not be succeed for some reasons. This is an un-safe type conversion.
Explicit
type conversion:
Ex-1:
using System;
namespace TypeConversionApplication
{
class ExplicitConversion
{
static void Main(string[]
args)
{
double
d = 6313.74;
int
i;
// cast
double to int.
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
OUTPUT:
6313
Ex-2:
using System;
namespace explicit_conversion
{
class Program
{
static void Main(string[]
args)
{
int
num = 65;
char
alpha;
alpha = (char)num;
// In this the int values
are explicitly converted to char data type;
//you have to tell
compiler to do the conversion, it uses casting.
Console.WriteLine("alphabet is: " + alpha);
Console.ReadLine();
}
}
}
Note: You can also use Explicit Cast Operator () and unboxing for
explicit conversion.
C# Type Conversion Methods
C#
provides the following built-in type conversion methods:
Sr.No
|
Methods
& Description
|
1
|
ToBoolean
Converts
a type to a Boolean value, where possible.
|
2
|
ToByte
Converts
a type to a byte.
|
3
|
ToChar
Converts
a type to a single Unicode character, where possible.
|
4
|
ToDateTime
Converts
a type (integer or string type) to date-time structures.
|
5
|
ToDecimal
Converts
a floating point or integer type to a decimal type.
|
6
|
ToDouble
Converts
a type to a double type.
|
7
|
ToInt16
Converts
a type to a 16-bit integer.
|
8
|
ToInt32
Converts
a type to a 32-bit integer.
|
9
|
ToInt64
Converts
a type to a 64-bit integer.
|
10
|
ToSbyte
Converts
a type to a signed byte type.
|
11
|
ToSingle
Converts
a type to a small floating point number.
|
12
|
ToString
Converts
a type to a string.
|
13
|
ToType
Converts
a type to a specified type.
|
14
|
ToUInt16
Converts
a type to an unsigned int type.
|
15
|
ToUInt32
Converts
a type to an unsigned long type.
|
16
|
ToUInt64
Converts
a type to an unsigned big integer.
|
The
following example is on conversin using conversion methods that converts value
types into string types:
Ex-1
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
Ex-2
using System;
namespace convert_conversion
{
class Program
{
static void Main(string[]
args)
{
//
example of using convert class
string
num = "23";
int
number = Convert.ToInt32(num);
int
age = 24;
string
vote = Convert.ToString(age);
Console.WriteLine("Your number is : " + number);
Console.WriteLine("Your voting age is : " + age);
Console.ReadLine();
}
}
}
Note: Upcasting and
Downcasting?
Implicit
conversion of derived classes to base class is called Upcasting and Explicit
conversion of base class to derived classes is called Downcasting.
Comments
Post a Comment