All things microsoft are filled with idiosyncrasies. But in this case casting and assignment are not related but are still required. Catch 22. One expression is evaluated before the assignement so that the reason for the doubling.
// This is required declaration.
HttpWebRequest request;
// This is after the declaration.
request = (HttpWebRequest)WebRequest.Create("
http://www.SomeDomain.com/");
// The equation does not matter.
// When performing them you may need to cast many times.
int j = 1;
double d = 1.1;
j = (int)d;
j = (int)(double)j * (int)d;
What you should be pissed off about is the lack of definitive syntax.
(type)object
(type)object + object2
(type)(object + object2)
Which is which? Experimentation reveals but no document explicitly says (type)(nearest_object) or (type)(all objects following till end of line ';'. Yet this fan boys will always root for their masters.
Terje wrote:
Casting syntax
28-Jul-08
I come from VB6 and finally decided to take a serious look at C#, but
the type casting really pisses me off. Why on earth would I want to
write syntax like this:
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("
http://www.SomeDomain.com/");
I can live with the curly brackets and the semicolons, but I'm not sure
I can live with this. Help ;-p
terje
Previous Posts In This Thread:
Casting syntax
I come from VB6 and finally decided to take a serious look at C#, but
the type casting really pisses me off. Why on earth would I want to
write syntax like this:
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("
http://www.SomeDomain.com/");
I can live with the curly brackets and the semicolons, but I'm not sure
I can live with this. Help ;-p
terje
Re: Casting syntax
Well, that's what the casting syntax is in C#. What do you prefer about
the VB syntax?
Note that with generics, you don't need to cast quite as often in C# 2
and 3 as you did in C# 1.
--
Jon Skeet - <
[email protected]>
Web site:
http://www.pobox.com/~skeet
Blog:
http://www.msmvps.com/jon.skeet
C# in Depth:
http://csharpindepth.com
Re: Casting syntax
Jon Skeet [C# MVP] skrev:
What bothers me is the fact that I have to define target datatype
*twice*. Why? ;-p
terje
Re: Casting syntax
In C#, why on earth _wouldn't_ you? That's how you convert from one type
to another.
Go back to using VB?
Getting "pissed off" or feeling like you're "not sure you can live with"
the language's syntax seems pretty silly to me. But if you insist, it
seems to me that the best solution is to just go back to using a language
you can tolerate.
Pete
Re: Casting syntax
For the purpose of the cast, you only need to specify the type _once_.
It's true that in the line of code you posted, you also have to specify
the type when declaring the variable. But that doesn't have anything to
do with the cast.
You will find that C# requires you to be more explicit about what you're
doing. However, you will also find that C# will almost never make an
inference that turns out to result in behavior you didn't mean to happen
(and even when it does, it will only be because you didn't fully
understand the inference/overloading rules, not because the language is
doing something complicated and unexpected on your behalf).
Pete
Re: Casting syntax
Would you not have to in VB.NET as well, at least with Option Strict
and Option Explicit on?
In fact, in C# 3 you don't have to - you can use an implicitly typed
local variable:
var request =3D
(HttpWebRequest)WebRequest.Create("
http://www.SomeDomain.com/");
Jon
Re: Casting syntax
(HttpWebRequest)WebRequest.Create("
http://www.SomeDomain.com/");
First: the language is not designed to make you do minimal typing - the
language is designed to force you to write robust and readable code.
Implicit conversions is not good for that. Actually C# allows you
to create implicit conversions. But it should and is only used
in special cases.
Secondly: the type of the variable and the type in the cast does
not need to be the same - the last just need to be assignable
or implicit convertable to the first.
Arne
You are correct/
All things microsoft are filled with idiosyncrasies. But in this case casting and assignment are not related but are still required. Catch 22. One expression is evaluated before the assignement so that the reason for the doubling.
// This is required declaration.
HttpWebRequest request;
// This is after the declaration.
request = (HttpWebRequest)WebRequest.Create("
http://www.SomeDomain.com/");
// The equation does not matter.
// When performing them you may need to cast many times.
int j = 1;
double d = 1.1;
j = (int)d;
j = (int)(double)j * (int)d;
What you should be pissed off about is the lack of definitive syntax.
(type)object
(type)object + object2
(type)(object + object2)
Which is which? Experimentation reveals but no document explicitly says (type)(nearest_object) or (type)(all objects following till end of line ';'. Yet this fan boys will always root for their masters.
EggHeadCafe - Software Developer Portal of Choice
Creating A Custom XML Web Control
http://www.eggheadcafe.com/tutorial...6-3399ea946e75/creating-a-custom-xml-web.aspx