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
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 - <sk...@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
What bothers me is the fact that I have to define target datatype
*twice*. Why? ;-p
terje
> 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/");
In C#, why on earth _wouldn't_ you? That's how you convert from one type
to another.
> I can live with the curly brackets and the semicolons, but I'm not sure
> I can live with this. Help ;-p
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
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
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 =
(HttpWebRequest)WebRequest.Create("http://www.SomeDomain.com/");
Jon
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
// 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
terje
Previous Posts In This Thread:
On Monday, July 28, 2008 11:54 AM
Terje wrote:
Casting syntax
terje
On Monday, July 28, 2008 12:12 PM
Jon Skeet [C# MVP] wrote:
Re: Casting syntax
Terje <fa...@usenet.no> wrote:
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 - <sk...@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
On Monday, July 28, 2008 12:40 PM
Terje wrote:
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
On Monday, July 28, 2008 12:45 PM
Peter Duniho wrote:
Re: Casting syntax
On Mon, 28 Jul 2008 08:54:57 -0700, Terje <fa...@usenet.no> wrote:
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
On Monday, July 28, 2008 1:01 PM
Peter Duniho wrote:
Re: Casting syntax
Pete
On Wednesday, July 30, 2008 4:54 AM
Jon Skeet [C# MVP] wrote:
Re: Casting syntax
On Jul 28, 5:40=A0pm, Terje <f...@usenet.no> wrote:
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
On Saturday, August 02, 2008 4:45 PM
arn wrote:
Re: Casting syntax
Terje wrote:
>>>>HttpWebRequest request =
(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
EggHeadCafe - Software Developer Portal of Choice
Custom Xml Serialization and storage of Classes in Config files
http://www.eggheadcafe.com/tutorials/aspnet/2143c159-8e1b-4cf0-85ed-75f91d57b381/custom-xml-serialization.aspx
// 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
terje
Previous Posts In This Thread:
On Monday, July 28, 2008 11:54 AM
Terje wrote:
Casting syntax
terje
On Monday, July 28, 2008 12:12 PM
terje
Pete
Pete
Jon
>>>>HttpWebRequest request =
(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
On Friday, November 20, 2009 12:52 PM
ACG wrote:
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/tutorials/aspnet/a13ca968-4f9e-4f11-a256-3399ea946e75/creating-a-custom-xml-web.aspx
"ACG" wrote:
> All things microsoft are filled with idiosyncrasies.
<snip>
>
> 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.
>
>
The C# Language Specification (Section 7.6) lists cast operators along with
+, -, !, ~, ++ and -- as unary operators. That pretty much clarifies the
above pieces of code.
Mike
<snip>
> 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 ';'.
On the contrary, section 7.2.1 of the C# language specification clearly
defines precendence and associativity. The unary expression (T)x has higher
precedence than the addition expression x + y, so (type)object + object2 is
equivalent to ((type)object) + object2, not (type)(object + object2). There's
no ambiguity at all.
Jon
It is hardly surprising that doing 3 different things
requires 3 slightly different syntaxes.
Besides the examples are rather bad code, so they have
relevance - it is very difficult to create a language that
prevent writing bad code.
> What you should be pissed off about is the lack of definitive syntax.
>
> (type)object
> (type)object + object2
> (type)(object + object2)
>
> Which is which?
The rules are well documented in the language spec.
And are BTW the same as for many other languages.
They should seem rather natural for any programmer.
Arne