Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When one type of data is assigned to another type an implicit conversion will take place automatically if

1>The two types are compatible

2>Destination type has a range that is greater than sourse type.
(reference from c# 4.0 complete reference.by Herbert Schildt)

Plese tell me meaning of first rule with simple example.
Posted

Here[^] you have all you need.
A 'simple example':
C#
int i = 5;
double f = i;
 
Share this answer
 
Comments
AmitGajjar 17-Oct-12 4:13am    
5+ exact link
C#
int num =  2147483647;
long bigNum = num;



or
C#
class Alpha
{
int data1;
int data2;

public static implicit Alpha(int n)
     {
     Alpha a=new Alpha();
     a.data1=n;
     return a;       //here the value of n fully becomes  part of the destination. ie. no data loss.
     }


public static explicit int(Alpha a)
     {
     return a.data1;  // here only data1 is returned. and data2 is   lost because of the conversion

     }

}




You can access the data as follows.


Alpha a = 25; //implicit conversion - no casting

//int n = a; // ERROR - without casting

int n = (int) a; //explicit conversion - with casting
 
Share this answer
 
v2
Comments
fdiu 17-Oct-12 3:41am    
is char and int are compatible ?
Hi,

it means if you have two jug of water, one with 5 liter and another with 2 liter then you can pour 2 liter jug liquid into 5 liter jug.

Similarly Please look into below example from MSDN
C#
Digit d = new Digit(3);
// implicit (no cast) conversion from Digit to byte
byte b = d;

In above example you can see, we can copy the value of Digit d into byte b. without explicitly casting. this is what the meaning of first phase.

Thanks.
 
Share this answer
 
Comments
fdiu 17-Oct-12 3:52am    
ur explanation i liked..
so if jug named char has capacity of 2 liter and other jug named ushort has capacity of also 2 liter then
why it not implicitly converted from ushort to char jug?
AmitGajjar 17-Oct-12 4:03am    
char is storing character value although internally it is storing integer value. so you think it should be case but you need to explicitly define that the value of ushort you are passing is actually character type. reverse will be possible without any issue. but to cast ushort into char needs explicitly casting.
fdiu 17-Oct-12 4:27am    
nice bro..
Thanks.
AmitGajjar 17-Oct-12 4:29am    
your welcome.
It means if the source type can be changed to the destination type without loss of data, then it can be done implicitly.

And example would be conversion from an int to a double:
C#
int i = 6;
double d = i;
This is an implicit conversion because no info is lost.
C#
double d = 6.0;
int i = d;
Will not, because any fraction part would have to be discarded.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900