Casting syntax

  • Thread starter Thread starter Terje
  • Start date Start date
T

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/");

I can live with the curly brackets and the semicolons, but I'm not sure
I can live with this. Help ;-p

terje
 
Terje said:
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

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 [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
 
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

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
 
Terje said:
Jon Skeet [C# MVP] skrev:
(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
 
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 ;-

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 ;-

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.skee
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 datatyp
*twice*. Why? ;-

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 Stric
and Option Explicit on

In fact, in C# 3 you don't have to - you can use an implicitly type
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

EggHeadCafe - Software Developer Portal of Choice
Custom Xml Serialization and storage of Classes in Config files
http://www.eggheadcafe.com/tutorial...ed-75f91d57b381/custom-xml-serialization.aspx
 
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
 
ACG said:
All things microsoft are filled with idiosyncrasies.

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
 
:

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
 
ACG said:
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
 
Back
Top