[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2006-06-02
Medium Priority
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

AlexFM earned 1000 total points
ID: 16819669
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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