Home » c# » Why you can’t cast an integer to a string.

Why you can’t cast an integer to a string.

I saw a question on Channel 9 that I’ve heard before. My guess is that there are enough people who have the same question that it’s worth addressing.

I know there’s probably a really good reason for this, but I can’t think of what it is, and it keeps bugging me. Why can’t you do
int x = 10;
string y = (string)x;
in C#? I mean, you could simply use x.ToString(), but why doesn’t the explicit cast do the same?

Why do you think you can do this in the first place?

The people who tend to ask this question are people coming out of a VB background. In VB, we have functions that will allow us to convert any type to any other type. CStr() is one of those functions. Give it an integer and it will convert it to a string. CInt() is another. Give it a value and it will convert it to an integer.

So, when people from that background first learn about casting, the only thing they have to hook the concept to in their thinking, is the conversion functions that are available in VB. You can’t blame them, really.

But a cast is not a conversion. A conversion operation actually returns a completely different value. To do a similar operation in CSharp, you’d actually use the functions in the Convert class.

A cast, on the other hand, doesn’t change the variable. It just lets us access it as though it were another type. And you can only cast if the data you are pointing to is related, via inheritance, to the type you want to cast it to.

So the simple answer is, “because an int is not a string”

When dealing with an object-oriented language, particularly .NET languages, everything except for the Object type derives from some other object. So technically, a string IS AN object, an int IS AN object, in fact, everything IS AN object.

If I have a class “Person” and a class named “Employee” that derives from “Person” and another class “Customer” that derives from “Person” I can say that an “Employee” is a “Person” and that a “Customer” is a “Person” but I can’t say that a “Customer” is an “Employee”. Neither can I say that a “Person” is a “Customer”, or that a “Person” is an “Employee”. It doesn’t make sense.

So, if I have an object of type Employee, I can cast that object to a type Person. But I can’t cast it to a type of Customer because there is no “is a” relationship between the two.

So, how does this relate to Strings and ints?

Well, if you look at the inheritance tree for .NET, you will see that a String inherits directly from the Object class. You will also see that an int inherits from the ValueType class, which inherits from the Object class. Since neither type is derived from the other, you can’t cast them–you can only convert them.

A little-known fact about VB.NET is that it also has a casting “function.” It looks like a function to the VB guys, but it is really an operator disguised as a function. directcast() takes an object as the first parameter and the type you want to cast it to as the second parameter. The operator does the same thing that the casting operator in CSharp does.

 

Other post in c#

Related Post

  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the […]
  • C# Properties Get and SetC# Properties Get and Set My son is learning to program.  Last week he asked me to explain C# properties get and set and, as it turns out, it looks like many others are asking for the same.  So, I’ve decided to […]
  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]
  • Magic Strings and Magic NumbersMagic Strings and Magic Numbers This past week a very old (last time I did work for him was in 2007) client of mine contacted me because their program suddenly started exhibiting a problem.  It seems that if a user […]
  • Running Selenium In Parallel With Any .NET Unit Testing ToolRunning Selenium In Parallel With Any .NET Unit Testing Tool Running Selenium in parallel from .NET seems to be a problem because, as of the time of this writing, I’ve yet to find a viable way of running selenium test on multiple browsers using […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.

One Pingback/Trackback

    13 January 2009 at 5:01am
    [...] Why you can’t cast an integer to a string. - To do a similar operation in ...
  • As is in CSharp too.
  • rsj

    This is really informative. Thank you! I thought that cast and conversion is just the same(convert any type to any other type). I thought that cast is the counterpart of cstr(),cint() etc on vb6. Thanks so much!

  • Pingback: As is in CSharp too.()