Click here to Skip to main content
15,880,469 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)

Now following this rule
char ch;
ushort vushort;
ch=vshort; //error:implicit conversion not from ushort to char

Now my question is that:
char is an unsigned 16 bit data.And ushort also unsigned 16 bit depth.
then why implicite conversion not possible.

Give answer in accordance to rule stated above..
Posted
Comments
Sushil Mate 17-Oct-12 4:01am    
you asking same question again n again?
Mario Majčica 17-Oct-12 4:11am    
Seems that my answer, answers to all of his questions :)
Sushil Mate 17-Oct-12 4:14am    
i have given him that solution too.. but he didn't marked as solution. :(
Mario Majčica 17-Oct-12 4:17am    
Well I just saw it. You got my 5 ;) (still you were not precise on answering why :P )
Sushil Mate 17-Oct-12 4:23am    
ohhh thank you.. i thought i was :P i missed this (Still, there is no implicit numeric conversion defined for ushort to char)
my +5 for that line :)

1 solution

Hi rtn,

This is a tricky question. Still, there is no implicit numeric conversion defined for ushort to char. The only valid thing is vice-versa, from char to ushort.
In short, there are no implicit conversions to char type, so the values of the other integral types do not automatically convert to the char type.

The implicit numeric conversions are:
  • From sbyte to short, int, long, float, double, or decimal.

  • From byte to short, ushort, int, uint, long, ulong, float, double, or decimal.

  • From short to int, long, float, double, or decimal.

  • From ushort to int, uint, long, ulong, float, double, or decimal.

  • From int to long, float, double, or decimal.

  • From uint to long, ulong, float, double, or decimal.

  • From long to float, double, or decimal.

  • From ulong to float, double, or decimal.

  • From char to ushort, int, uint, long, ulong, float, double, or decimal.

  • From float to double.


For example, this will work just fine:

C#
char ch = '1';
ushort vushort;
vushort = ch;



Hope this helps.

Cheers
 
Share this answer
 
v4
Comments
fdiu 17-Oct-12 4:09am    
Hi Mario,
thanks for reply.
Mario Majčica 17-Oct-12 4:12am    
You are welcome.

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