Jump to content

Confused on explicit and implicit conversions

- - - - -

  • Please log in to reply
1 reply to this topic

#1
demo53

demo53

    Newbie

  • Members
  • PipPip
  • 10 posts
Alright let me recap what I know about the two so if I'm wrong about my understanding, please tell me!

Okay, I understand that when you are performing an implicit conversion, you are converting a type into another type what can hold the same amount of numbers. So a char into a short would be an implicit conversion.

Explicit conversion is when you are performing a conversion where you are changing a type into another type in which the two cannot hold the same amount of numbers. So an explicit conversion would be changing a short into a byte.

Well now when my book shows this:
          doubleResult = floatVal * shortVal;

It says that C# will do an implicit conversion to change the shortVal into a float before assigning it to the doubleResult value. Well, wouldn't that be an explicit conversion changing the shortVal into a float?

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Nope. For an explicit conversion you'll see type information:
int a = 5;

double b = a; // Implicit conversion

int c = (int)b; // Explicit conversion, I have to tell it what type I want to end up with





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users