Array casting bug??

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

    Array casting bug??

    If variance/covariance is not allowed in array of value types, why is the
    expression on the "Main" method run successfully (see snippet below)? I am
    missing something?

    Thank you.

    ---------------------
    class Program
    {
    enum Foo
    {
    a, b, c
    }

    enum Bar
    {
    x, y, z
    }

    static void Main(string[] args)
    {
    Bar[] barArray = (Bar[])System.Enum.Ge tValues(typeof( Foo));
    }
    }
    ---------------------

  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: Array casting bug??

    If you want an array of the Enum type, you have to loop. If you want an
    array of strings, you can use GetValues or GetNames, depending on what you
    are aiming at.

    I can't think of any other way off hand.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "Rene" <a@b.comwrote in message
    news:uTW$qaoKJH A.3744@TK2MSFTN GP06.phx.gbl...
    If variance/covariance is not allowed in array of value types, why is the
    expression on the "Main" method run successfully (see snippet below)? I am
    missing something?
    >
    Thank you.
    >
    ---------------------
    class Program
    {
    enum Foo
    {
    a, b, c
    }
    >
    enum Bar
    {
    x, y, z
    }
    >
    static void Main(string[] args)
    {
    Bar[] barArray = (Bar[])System.Enum.Ge tValues(typeof( Foo));
    }
    }
    ---------------------
    >

    Comment

    • Peter Duniho

      #3
      Re: Array casting bug??

      On Thu, 09 Oct 2008 20:16:57 -0700, Rene <a@b.comwrote :
      If variance/covariance is not allowed in array of value types, why is
      the expression on the "Main" method run successfully (see snippet
      below)? I am missing something?
      I think so. But it's not necessarily something obvious. :)

      A question nearly the same as yours came up a few months ago. You might
      find that thread relevant in addressing your question:


      The big difference between that thread and your question is that in the
      previous thread, the person was using the "as" operator, whereas you're
      using a cast. But I think in this particular example, the two are
      basically equivalent (except for what happens on an error). That is, the
      statement is legal C# and the error-checking is done by the CLR, which
      allows it because the underlying enum types are the same.

      In other words, I think that this particular array cast is in fact legal.
      It's not a bug for it to work.

      Pete

      Comment

      • Rene

        #4
        Re: Array casting bug??

        Hi Greg,

        What I wanted to know is why the code is not throwing an error. Casting
        from Foo[] to Bar[] is illegal so Enum.GetValues( ) should throw an error
        when it tries to cast the return value Foo[] (Array) to Bar[].

        At least that's why I think, I am sure I am probably wrong about my
        assumption. That is why I would like to know if someone could clarify.

        Thanks.



        "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld @comcast.netNoS pamMwrote in
        message news:%23eR1mloK JHA.5232@TK2MSF TNGP05.phx.gbl. ..
        If you want an array of the Enum type, you have to loop. If you want an
        array of strings, you can use GetValues or GetNames, depending on what you
        are aiming at.
        >
        I can't think of any other way off hand.
        >
        --
        Gregory A. Beamer
        MVP, MCP: +I, SE, SD, DBA
        >
        Subscribe to my blog

        >
        or just read it:

        >
        *************** *************** **************
        | Think outside the box! |
        *************** *************** **************
        "Rene" <a@b.comwrote in message
        news:uTW$qaoKJH A.3744@TK2MSFTN GP06.phx.gbl...
        >If variance/covariance is not allowed in array of value types, why is the
        >expression on the "Main" method run successfully (see snippet below)? I
        >am missing something?
        >>
        >Thank you.
        >>
        >---------------------
        >class Program
        >{
        > enum Foo
        > {
        > a, b, c
        > }
        >>
        > enum Bar
        > {
        > x, y, z
        > }
        >>
        > static void Main(string[] args)
        > {
        > Bar[] barArray = (Bar[])System.Enum.Ge tValues(typeof( Foo));
        > }
        >}
        >---------------------
        >>
        >

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Array casting bug??

          Rene <a@b.comwrote :
          What I wanted to know is why the code is not throwing an error. Casting
          from Foo[] to Bar[] is illegal so Enum.GetValues( ) should throw an error
          when it tries to cast the return value Foo[] (Array) to Bar[].
          >
          At least that's why I think, I am sure I am probably wrong about my
          assumption. That is why I would like to know if someone could clarify.
          A very similar question has come up before, and I asked Eric Lippert
          (on the C# team) about it. Basically C# "trusts" the CLR to permit/deny
          appropriate conversions, and on this front the CLR is slightly more
          permissive than we'd expect.

          Here's the previous thread:


          /browse_thread/thread/2d21bf036a23918 e/5a5c351206ebd99 9

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

          Working...