Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Casting syntax

0 views
Skip to first unread message

Terje

unread,
Jul 28, 2008, 11:54:57 AM7/28/08
to
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

Jon Skeet [C# MVP]

unread,
Jul 28, 2008, 12:12:44 PM7/28/08
to

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

Terje

unread,
Jul 28, 2008, 12:40:18 PM7/28/08
to
Jon Skeet [C# MVP] skrev:

> Well, that's what the casting syntax is in C#. What do you prefer about
> the VB syntax?

What bothers me is the fact that I have to define target datatype
*twice*. Why? ;-p

terje

Peter Duniho

unread,
Jul 28, 2008, 12:45:36 PM7/28/08
to
On Mon, 28 Jul 2008 08:54:57 -0700, Terje <fa...@usenet.no> wrote:

> 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

Peter Duniho

unread,
Jul 28, 2008, 1:01:58 PM7/28/08
to

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

Jon Skeet [C# MVP]

unread,
Jul 28, 2008, 1:17:10 PM7/28/08
to

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

Arne Vajhøj

unread,
Aug 2, 2008, 4:45:13 PM8/2/08
to
Terje wrote:
> Jon Skeet [C# MVP] skrev:
>>>>HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://www.SomeDomain.com/");
>> Well, that's what the casting syntax is in C#. What do you prefer
>> about the VB syntax?
>
> What bothers me is the fact that I have to define target datatype
> *twice*. Why? ;-p

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

ACG

unread,
Nov 20, 2009, 12:52:20 PM11/20/09
to
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

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.

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

ACG

unread,
Nov 20, 2009, 12:53:07 PM11/20/09
to
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

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

Family Tree Mike

unread,
Nov 20, 2009, 2:59:02 PM11/20/09
to

"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

Jon Skeet [C# MVP]

unread,
Nov 21, 2009, 1:10:04 PM11/21/09
to
"ACG" wrote:

<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

Arne Vajhøj

unread,
Nov 22, 2009, 10:25:43 PM11/22/09
to
ACG wrote:
> 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;

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

0 new messages