in this post I'm going to talk about various kinds of type conversion in c-sharp we have implicit type conversion explicit type conversion which is also called casting and we also have conversion between non compatible types here is an example of implicit type conversion a blight as you know takes only one byte of memory and an integer takes four bytes so we can easily copy a byte to an integer what happens at runtime is let's take a look at this slide so here's the binary representation of our B variable here so one is represented as seven bits of zero and one bit of one when we copy a byte to an integer what the runtime does is it prefixes that value with a bunch of zeros to fill the four bytes so there is no data loss situations like that when the compiler is 100% sure that the types are compatible and no data loss will happen values can be converted to a different type implicitly here is another example of implicit type conversion so we have an integer set it to one and we copy that to a float again in this example no data loss will happen but let's take a look at this one here we have declared an integer and we're trying to copy that to a byte and integer is four bytes so when we convert that to a byte three bytes out of four bytes will be gone and there is a chance for data loss now data loss doesn't always happen it only happens if the value restored in the integer is beyond the capacity of a byte in this example one can be stored in a byte so no data loss will happen but if we had let's say 300 here we cannot store 300 in a byte so as a result of that conversion data will be lost when the compiler knows that there is a chance for data loss it doesn't allow implicit type conversion and you need to explicitly tell the compiler that you're aware of the data loss and you still want to go ahead with the conversion in situations like that what we do is we prefix the variable with the target type so here I'm trying to convert I to a byte this is what we call as casting here is another example so we have a float set to 1.0 and if we try to convert that to an integer the compiler will complain I will show you that later in the coding demo so we need to tell the compiler that we are aware of the data loss and we still want to convert F which is a float to an integer so we cast it like here sometimes we are working with types that are not compatible but you still need to convert them for example we might have a number represented as a string as you see here and we need to convert it to an integer in situations like that because string and int are not compatible they cannot use explicit casting so we need a different mechanism for converting a string to a number in situations like that we need to use the convert class or use the parse method so convert class is part of data framework and is defined in the system namespace it has a bunch of methods for converting various types to other types and they all start with two in this case we're trying to convert s which is a string to an int 32 in 32 as you know is a dotnet framework type which maps to a city ARP integer type remember a byte is one byte a short is two bytes and integer is 4 bytes and a long is eight bytes you probably know that each byte has 8 bits so an integer which has four bytes times 8 bits ends up being 32 bits that's why is called 2 in 32 an int 16 which represents 16 bits equals to short which is 2 bytes we also have this parse method here all the primitive types that I explained in the last lecture like integer long float boolean they all have this parse method and the parse method takes a string and tries to convert that to the target type in this case an integer here are some of the methods that you can find in the convert class which converts the given value to a byte 2 in 16 to convert the given value to a short 2 in 32 to convert the given value to an integer and to in 64 to convert the given value to a long okay in a theory let's jump into code and see all this concept in action
Post a Comment
If you have any doubt please let me know