Is .NET retarded when it comes to casting types or is it me?

Posted on 2006-06-02
Last Modified: 2010-04-16
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?

Question by:pjsulin
    1 Comment
    LVL 48

    Accepted Solution

    1. Because this is not casting - c is string, "1234" is constant of string type.
    2. No, except of syntax difference.
    3. String can contain non-digit symbols like "abc". This is why we need to use Parse function and handle exceptions which it throws.

    Type safety is important feature of real programming language (vs. scripting), it allows to avoid runtime errors.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    WiX Installer 2 40
    How to utilize an uploaded VHD as a template in Azure 6 59 mvc 2 30
    send mail from 8 12
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now