Converting - Implicit

Also known as Type Coercion.
An implicit data type conversion is a conversion that happens automatically. Widening casts are often implicit.
C# provides implicit conversion for a large number of data types.


Integer to Long

int myInteger; 
long myLong;
myInteger = 10;
myLong = myInteger;
Console.WriteLine(myLong);

Double to Integer

double myDouble 
int myInteger;
myDouble = 10;
myInteger = myDouble;
Console.WriteLine(myInteger);

Integer to Boolean

int myInteger; 
bool myBool;
myInteger = 10;
myBool = myInteger;
Console.WriteLine(myBool);

Boolean to Integer

bool myBool; 
int myInteger;
myBool = true;
myInteger = myBool;
Console.WriteLine(myInteger);

String to Long

string myText; 
long myLong;
myText = "10";
myLong = myText;
Console.WriteLine(myLong);

Long to Integer - Overflow

long myLong; 
int myInteger;
myLong = 10;
myInteger = myLong;
Console.WriteLine(myInteger);

Conversion is perfomed automatically.


This includes arithmetic operation. Is invoked automatically when a value of one data type is assigned to a value of another.
No special syntax is required because the conversion is type safe and no data will be lost.
Examples include conversions from smaller to larger integral types

short myShort = 5; 
int myInt = myShort;

This is an example of a widening conversion.


For classes there is always an implicit conversion from a Derived class to a Base class.
A derived class always contains all the members of its base class.

public class Base {} 
public class Derived : Base {}
Base B = new Derived();

C# will automatically convert an integer variable to a string when it is concatenated using the + operator.
Examples include converting from smaller to larger integral types and converting from a derived class to a base class.
For built-in numeric types an implicit conversion can be made when the value can fit into the variable without being truncated or rounded off
There are no implicit conversions to the char type
There are no implicit conversions between floating-point and decimal types


int myInteger = 1234; 
long myLong = myInteger;

FromTo
Sbyte short , int, long, float, double, or decimal
Byte - short , ushort, int, uint, long, ulong, float, double, or decimal
Short - int , long, float, double, or decimal
Ushort - int , uint, long, ulong, float, double, or decimal
Int - long , float, double, or decimal
Uint - long , ulong, float, double, or decimal
Long - float , double, or decimal
Char - ushort , int, uint, long, ulong, float, double, or decimal
Float - double
Ulong - float , double, or decimal

VB.Net

There is no implicit data type conversion in VB.Net it must be explicit using the ToString method.



Widening conversion - This is one in which the conversion is to a data type that can accommodate every possible value.
A widening conversion can be implicit or explicit.
For example a Long can accommodate every possible value held by an Integer. So Integer to Long is widening.
Narrowing conversion - This is one in which the conversion is to a data type that does not accommodate every possible value.
A narrowing conversion is always explicit.
For example an Integer cannot accommodate every possible value held by a Long. So Long to Integer is narrowing.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext