Question

How do you go about casting numbers to specific data types? Example: 253 is 32-bit signed integer 253L is 64-bit signed integer 253D is Double precision float As you can see you can cast directly to Long and Double, but there are certain problems I have here. I cannot cast to byte, single, 16bit, unsigned...

It becomes a problem when I have to input data into many different functions with arguments of varying data types: Method1( byte Value ); Method2( sbyte Value ); Method3( ushort Value ); //Etc.

Était-ce utile?

La solution

Using int.Parse(string) or Convert.ToInt32 will do the trick. Or you could try casting the value expicitly like that:

int age = 53;

Method1((byte) age);

Autres conseils

Try using the Convert class:

http://msdn.microsoft.com/en-us/library/System.Convert_methods(v=vs.110).aspx

e.g.

int myInt = Convert.ToInt32(anything);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top