Casting syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terje

    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.Cre ate("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]

    #2
    Re: Casting syntax

    Terje <fake@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.Cre ate("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 - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Terje

      #3
      Re: Casting syntax

      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

      Comment

      • Peter Duniho

        #4
        Re: Casting syntax

        On Mon, 28 Jul 2008 08:54:57 -0700, Terje <fake@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.Cre ate("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

        Comment

        • Peter Duniho

          #5
          Re: Casting syntax

          On Mon, 28 Jul 2008 09:40:18 -0700, Terje <fake@usenet.no wrote:
          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?
          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

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Casting syntax

            On Jul 28, 5:40 pm, Terje <f...@usenet.no wrote:
            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.Cre ate("http://www.SomeDomain. com/");

            Jon

            Comment

            • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

              #7
              Re: Casting syntax

              Terje wrote:
              Jon Skeet [C# MVP] skrev:
              >>>>HttpWebRequ est request =
              (HttpWebRequest )WebRequest.Cre ate("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

              Comment

              Working...