Is .NET retarded when it comes to casting types or is it me?
Posted on 2006-06-02
The thing I notice most every single day while programming in C# is how annoying it is to have to cast every variable I use ALL THE TIME. Not only am I constantly casting my vars, but I am continously amazed that .NET throws so many casting errors. For example,
string x = "1234";
int x = (int) x; // This doesn't work so I have to do this
int x = int.Parse(x.ToString())
I understand that the .NET platform - interoperability between languages, reflection, etc -all rely on a strongly typed language but it seems to be a little overdone.
My questions are:
1. Why does the first casting not work in the above example?
2 Is there any difference between c# and VB.NET with regard to casting, ie will vb.NET explicitly cast the obvious (I know the 'the obvious' is loaded term)?
3. Why do I have to use the Parse method on what seem to be trivial castings?
4. Is question 1 the same as 3?