Type Casting
Def: The process of converting the value from one data type to another data type iscalled as Casting. This is of two types.
- Implicit Casting:
- Explicit Casting:
Implicit Casting
Implicit casting is possible in the following cases.- Any numerical value from lower to higher type.
short to int
float to double
etc.
- Any numerical value from non-decimal type to decimal type.
long to double
etc.
The following table shows the all possible implicit conversions supported by C#
Implicit Casting in C#:
Explicit Casting
Explicit casting should be performed in the following cases:
- Any numerical value from higher type to lower type.
- Any numerical value from decimal type to non-decimal type.
- Any value from numerical type to non-numerical type.
- Any value from non-numerical type to numerical type.
- Syn: (target data type)variable
- (target data type)value
Download above Code of Demo of Casting : Download Links
Explicit Casting using Conversion Methods
Explicit casting can also be performed by using the ―Conversion methods‖. The ―System.Convert‖class provides several methods to perform explicit casting.
- System.Convert.ToSByte(value);
- System.Convert.ToByte(value);
- System.Convert.ToInt16(value);
- System.Convert.ToUInt16(value);
- System.Convert.ToInt32(value);
- System.Convert.ToUInt32(value);
- System.Convert.ToInt64(value);
- System.Convert.ToUInt64(value);
- System.Convert.ToSingle(value);
- System.Convert.ToDouble(value);
- System.Convert.ToDecimal(value);
- System.Convert.ToChar(value);
- System.Convert.ToString(value);
- System.Convert.ToBoolean(value);
No comments:
Post a Comment