![]() One of the best things in C# is the fact that they make casting pretty easy: there are a lot of methods out there to do casting, besides doing the () thing. You need to just access a single character in the string rather than try to convert the entire thing into a single character. You also can't cast a string to a character- a string, if you recall, is an array of characters. If you make a mistake and try to do something like int i = (int)IMyBeaconVariable the compiler will helpfully say, "Hey, you know this isn't going to work, right?" in its own, cold way. I can't cast a IMyBeacon to an int- they are inherently incompatible, since IMyBeacon is a class and is far too complex to be boiled down to an integer. Not everything is castable to something else. ![]() You do have to make sure that what you are casting is compatible, however. The best way to summarize casting: Casting is the act of telling the system that yes, I really did mean to do that, and no, you don't need to tell me that these two variables aren't the same type, thank you. Note: The example I used is definitely not an optimal solution, but this is about casting and not how to overload a method, which comes later, when I talk about methods. 0 being there and would return an error saying, "hey, you know this isn't an int you are saving to an int, right?") ![]() ![]() Semantics, really, but the computer cares about the. Then the result, which is returned as a double (so in this case, it would come back as 2.0) is cast into an int (now it becomes 2. So what did I do there? I made a single method (DivideTwoNumbers) and asked it to take two var variables (remember, var is context-sensitive- it automatically types the variable as to what is assigned to it), then divide one into the other, while converting the second number explicitly into a double (casting the variable as a double, basically). Public double DivideTwoNumbers(var i, var j) ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |