473,390 Members | 842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

casting vs. using the "Convert" methods?


Hi everyone,

A basic one here, I think. I haven't found the pattern yet, but
sometimes when I cast a variable to another type using the "C" style cast
operator the compiler refuses to play along. It says it's an invalid cast.
However, if I use the Convert.ToInt32() method (for example) things will
work. At least, that's how it appears to me.

Could someone explain when to use old-style parenthesized casts vs. the
Convert() methods?
Nov 16 '05 #1
7 30737
For the most part, casting says "This object of type A is really an
object of type B-derived-from-A"
Convert.To*() functions say This object isn't a type B, but there exists
a way to convert to type B"

string a = "1234";
object b = a;
string c = (string) b; // succeeds because b really
is a string.
// int d = (int) b; // fails because b is not an int.
int e = Convert.ToInt32(b); // Succeeds because b can be converted
into an int.

"Jim Bancroft" <as******@nowhere.com> wrote in message
news:#w**************@tk2msftngp13.phx.gbl...

Hi everyone,

A basic one here, I think. I haven't found the pattern yet, but
sometimes when I cast a variable to another type using the "C" style cast
operator the compiler refuses to play along. It says it's an invalid cast. However, if I use the Convert.ToInt32() method (for example) things will
work. At least, that's how it appears to me.

Could someone explain when to use old-style parenthesized casts vs. the Convert() methods?

Nov 16 '05 #2
Jim Bancroft wrote:
Hi everyone,

A basic one here, I think. I haven't found the pattern yet, but
sometimes when I cast a variable to another type using the "C" style cast
operator the compiler refuses to play along. It says it's an invalid cast.
However, if I use the Convert.ToInt32() method (for example) things will
work. At least, that's how it appears to me.

Could someone explain when to use old-style parenthesized casts vs. the
Convert() methods?


Basically, a cast does not really convert anything but only tells the
compiler to treat something as something "similar" - it just gets a
different internal type label, seen from the compiler's standpoint.
Converting data usually actually changes the underlying data structure,
casting does not do this.

Alexander
Nov 16 '05 #3
Not true. The truth is a bit more complex than that. .NET provides
three methods of getting from point A to point B, as it were.

First, there is the implicit cast. This is the cast that doesn't
require you to do anything more than an assignment:

int i = 5;
double d = i;

These are also called "widening conversions" and .NET allows you to
perform them without any cast operator because you could never lose any
information doing it: the possible range of valid values of a double
encompasses the range of valid values for an int and then some, so
you're never going to do this assignment and then discover to your
horror that the runtime dropped a few digits off your int value. For
reference types, the rule behind an implicit cast is that the cast
could never throw an InvalidCastException: it is clear to the compiler
that the cast is always valid.

You can make new implicit cast operators for your own types (which
means that you can make implicit casts that break all of the rules, if
you're stupid about it). The basic rule of thumb is that an implicit
cast can never include the possibility of losing information in the
transition.

Note that the underlying representation _did_ change in this
conversion: a double is represented completely differently from an int.

The second kind of conversion is an explicit cast. An explicit cast is
required wherever there is the possibility of losing information, or
there is a possibility that the cast might not be valid and thus throw
an InvalidCastException:

double d = 1.5;
int i = (int)d;

Here you are obviously going to lose information: i will be 1 after the
cast, so the 0.5 gets lost. This is also known as a "narrowing"
conversion, and the compiler requires that you include an explicit cast
(int) to indicate that yes, you know that information may be lost, but
you don't care.

Similarly, with reference types the compiler requires explicit casts in
situations in which the cast may not be valid at run time, as a signal
that yes, you know there's a risk, but you know what you're doing.

The third kind of conversion is one that involves such a radical change
in representation that the designers didn't provide even an explicit
cast: they make you call a method in order to do the conversion:

string s = "15";
int i = Convert.ToInt32(s);

Note that there is nothing that absolutely requires a method call here.
Implicit and explicit casts are method calls too (that's how you make
your own). The designers could quite easily have created an explicit
cast operator that converted a string to an int. The requirement that
you call a method is a stylistic choice rather than a fundamental
requirement of the language.

The stylistic reasoning goes something like this: String-to-int is a
complicated conversion with lots of opportunity for things going
horribly wrong:

string s = "The quick brown fox";
int i = Convert.ToInt32(s);

As such, the method call gives you documentation to read, and a broad
hint that this is something more than just a quick cast.

When designing your own types (particularly your own value types), you
may decide to create cast operators and conversion functions. The lines
dividing "implicit cast", "explicit cast", and "conversion function"
territory are a bit blurry, so different people may make different
decisions as to what should be what. Just try to keep in mind
information loss, and potential for exceptions and invalid data, and
that should help you decide.

Nov 16 '05 #4
Bruce Wood wrote:
Not true. The truth is a bit more complex than that. .NET provides
three methods of getting from point A to point B, as it were.
When I was talking of casting I was of course only talking about casting
in terms of objects and not of primitive types.

Note that the underlying representation _did_ change in this
conversion: a double is represented completely differently from an int.
Well, because it was a conversion and not a cast.

Note that there is nothing that absolutely requires a method call here.
The designers could quite easily have created an explicit
cast operator that converted a string to an int. The requirement that
you call a method is a stylistic choice rather than a fundamental
requirement of the language.
As proven by PHP.
Implicit and explicit casts are method calls too (that's how you make
your own).


I have to say I am not extremely well familiar with C# internals but I
doubt it calls a method for a simple cast. Casting an instance from
object to string will probably only tell the compiler not to report the
typical incompatible types message - along with additional checks for
possible unsupported casting exceptions.
Nov 16 '05 #5
I have to say I am not extremely well familiar with C# internals but I
doubt it calls a method for a simple cast. Casting an instance from
object to string will probably only tell the compiler not to report the
typical incompatible types message - along with additional checks for
possible unsupported casting exceptions.
Hmmm... I'm not sure about the C# compiler, and even less sure about
the JIT compiler (remember the C# 'compiler' actually translates source
code into MSIL !), but if you had to design the compiler, wouldn't you
simply make it instantiate an 'anonymous' instance of the class that
the object needs to be converted to and use that instance from that
point on (till end of scope, evidently) ?

I got a feeling it might be more complicated to tell the compiler not
to report errors than to make an instance of the casted-to class and
use that (this second alternative sounds safer to me too, on top of
that).

F.O.R.

Nov 16 '05 #6
Olorin wrote:

Hmmm... I'm not sure about the C# compiler, and even less sure about
the JIT compiler (remember the C# 'compiler' actually translates source
code into MSIL !), but if you had to design the compiler, wouldn't you
simply make it instantiate an 'anonymous' instance of the class that
the object needs to be converted to and use that instance from that
point on (till end of scope, evidently) ?

I got a feeling it might be more complicated to tell the compiler not
to report errors than to make an instance of the casted-to class and
use that (this second alternative sounds safer to me too, on top of
that).

F.O.R.


This might be very well possible, as I said I do not really know how it
is internally handled.

The only thing I would wonder about, would be that by instantiating a
second instance with absolutely the indentical instance data (not only
member values but also all instance states like locks, ....) it might
cause some confusion. I'd see it as easier to simply perform a check
whether the assigned class is compatible with the cast and do it if
everything is okay.
Nov 16 '05 #7
>> Not true. The truth is a bit more complex than that. .NET provides
three methods of getting from point A to point B, as it were.
When I was talking of casting I was of course only talking about casting in terms of objects and not of primitive types.
Sorry. I misunderstood, as the OP was giving examples of primitive type
casts. My reply was more directed at the OP than at your post.
I have to say I am not extremely well familiar with C# internals but I doubt it calls a method for a simple cast. Casting an instance from
object to string will probably only tell the compiler not to report the typical incompatible types message - along with additional checks for possible unsupported casting exceptions.


What you say is true for casts up and down the reference type
hierarchy, but not for other kinds of "casts" (which are really
conversions, as you pointed out, but which use the cast notation and
therefore are rightly referred to as "casts").

Casting a reference type to one of its parent types (up the class
hierarchy) or to a child type (down the class hierarchy) does not
involve anonymous instances or any copying at all. It simply, as you
mentioned, copies the reference to a variable of another type. Both
variables continue to point at the same object instance, and will
usually interpret all method calls and property references the same
(unless a method or property is declared "new").

When I was talking about casting to a string (or from a string), I was
not referring to a simple cast down the inheritance hierarchy like
this:

Object o = "abcdef";
string s = (string)o;

which in fact does nothing more than copy the reference to "abcdef"
from o to s, along with the necessary run-time type compatibility
checking. I was, instead, referring to a cast like this:

MyClass mc = new MyClass();
string s = (string)mc;

which in C# is possible if you declare an explicit or implicit cast
operator that casts from MyClass to a string. That "cast" is, in fact,
a call to the static method you declare that performs the conversion.
(If the cast operator is implicit then you can express the second line
like this:

string s = mc;

with no complaint from the compiler. Only explicit casts _require_ the
(string) cast notation.)

Of course, when dealing with built-in primitive types and casts up and
down the class hierarchy the compiler doesn't bother calling a method.
A method call is required only for casts that you define yourself.

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael Stevens | last post by:
Probably the wrong wording but since I'm not a scripter I won't claim to know what I'm talking about. I got this script from www.htmlgoodies.com <script language="JavaScript"> <!--...
13
by: dee | last post by:
Hi My code complies the following line: Session("passed") = 1 but puts wiggly error line under the second Session("passed") in the following expression: Session("passed") = Session("passed") +...
10
by: David Garamond | last post by:
The Postgres manual says: The AT TIME ZONE construct allows conversions of time stamps to different time zones. I'd guess most people would think what's meant here is something like "unit...
2
by: Matt Brown - identify | last post by:
Hello, I decided to not spend the rest of my life figuring out docking and, instead, use the cDockingHandler class offered on Veign.com (http:// www.veign.com/vrc_codeview.asp?type=app&id=149)....
6
by: arti | last post by:
I dont want to use Convert(Char(9),date,106) function to show date in dd/MM/yyyy format. It changes the datatype of my column to char & I cant perform other date operations on it without changing it...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.