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#
- Assign Multiple enum Values To One Variable - November 2nd, 2007
- Simple Properties in C# 3.5 - November 22nd, 2007
- CSharp adds the var keyword! - November 27th, 2007
- Object Initialization in CSharp 3.0 and VB.NET 9 - November 28th, 2007
- Computed Columns Using LINQ to SQL Classes - February 12th, 2008
- DataSets, ObjectDataSource and 3-Tier ASP.NET - February 29th, 2008
- Serializable attribute vs Serializable Interface - April 9th, 2008
- Why you can't cast an integer to a string. - June 23rd, 2008
- Unsafe Mode in C# - December 15th, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- Upload a File via WebRequest Using CSharp - November 10th, 2009
- Azure CDN Blob Storage Case Sensitive Issue - April 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Decimal vs Float (Single) or Double - September 18th, 2012
- C# Self Executing Anonymous Function - February 28th, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
- CSharp Enum - October 2nd, 2013
- CSharp Numeric Overflows - July 3rd, 2014
- CSharp IDisposable Confusion - July 17th, 2014
- Why Get Certified? - July 24th, 2014
- C# Properties Get and Set - November 13th, 2014
- Value Type vs Reference Type - November 27th, 2014
- String and StringBuilder - December 11th, 2014
- Browser Automation in .NET w/ Chromium - July 9th, 2015
- CefSharp Offscreen [Why do I have so many instances of Chromium?] - October 1st, 2015