Array element type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jtougas@gmail.com

    #1

    Array element type

    What is the way to the type of an array's elements?

    Array a = new long[] { 1, 2, 3 };
    Type t = a.GetType();
    Console.WriteLi ne( t.FullName );

    This will write "System.Int 64[]", but is not what I'm looking for.
    Rather I want the type of the array's elements, so "System.Int 64". I
    seem to remember something along the lines of "a.GetElementTy pe()" but
    I can 't find anyting of the sort anymore.

    Cheers

  • jtougas@gmail.com

    #2
    Re: Array element type

    Nevermind, found it...

    Array a = new long[] { 1, 2, 3 };
    Type t = a.GetType().Get ElementType();
    Console.WriteLi ne( t.FullName );

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Array element type

      <jtougas@gmail. com> wrote:[color=blue]
      > What is the way to the type of an array's elements?[/color]

      Type.GetElement Type()
      [color=blue]
      > Array a = new long[] { 1, 2, 3 };
      > Type t = a.GetType();
      > Console.WriteLi ne( t.FullName );
      >
      > This will write "System.Int 64[]", but is not what I'm looking for.
      > Rather I want the type of the array's elements, so "System.Int 64". I
      > seem to remember something along the lines of "a.GetElementTy pe()" but
      > I can 't find anyting of the sort anymore.[/color]

      Right method, wrong type :)

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Stoitcho Goutsev \(100\)

        #4
        Re: Array element type

        Hi,

        What about

        Type t = a[0].GetType();

        The method I believe you said you forgot I guess is

        Type t = a.GetType().Get ElementType();

        GetElementType is method of the Type class not the Array class.


        --
        HTH
        Stoitcho Goutsev (100)

        <jtougas@gmail. com> wrote in message
        news:1138308044 .407420.120080@ g49g2000cwa.goo glegroups.com.. .[color=blue]
        > What is the way to the type of an array's elements?
        >
        > Array a = new long[] { 1, 2, 3 };
        > Type t = a.GetType();
        > Console.WriteLi ne( t.FullName );
        >
        > This will write "System.Int 64[]", but is not what I'm looking for.
        > Rather I want the type of the array's elements, so "System.Int 64". I
        > seem to remember something along the lines of "a.GetElementTy pe()" but
        > I can 't find anyting of the sort anymore.
        >
        > Cheers
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Array element type

          Stoitcho Goutsev (100) <100@100.com> wrote:[color=blue]
          > What about
          >
          > Type t = a[0].GetType();[/color]

          There are three problems with that:

          1) The array might be empty
          2) For reference types, the first element might be null
          3) For reference types, the type of the actual object referred to could
          be derived from the type of the array element (eg object[] with the
          first element being a string)
          [color=blue]
          > The method I believe you said you forgot I guess is
          >
          > Type t = a.GetType().Get ElementType();
          >
          > GetElementType is method of the Type class not the Array class.[/color]

          Yup.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Stoitcho Goutsev \(100\)

            #6
            Re: Array element type

            >> What about[color=blue][color=green]
            >>
            >> Type t = a[0].GetType();[/color]
            >
            > There are three problems with that:
            >
            > 1) The array might be empty
            > 2) For reference types, the first element might be null
            > 3) For reference types, the type of the actual object referred to could
            > be derived from the type of the array element (eg object[] with the
            > first element being a string)
            >[/color]

            Yes, I know that. I was refering to the concrete example. That was most like
            a joke :) This was the reason I gave the real solution too.
            Thanks for the clarification.


            --

            Stoitcho Goutsev (100)


            Comment

            Working...