HasElementType

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

    #1

    HasElementType

    Type.HasElement Type -
    "Gets a value indicating whether the current Type encompasses or refers to
    another type; that is, whether the current Type is an array, a pointer, or
    is passed by reference." So why does this return false? String is passed
    by ref and is a ref type.

    string s = "text";
    Console.WriteLi ne("String hasElement:" + s.GetType().Has ElementType);

    Output:
    String hasElement:Fals e

    --
    William Stacey, MVP



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: HasElementType

    William,

    In the documentation for the HasElementType property, the meaning of
    "reference" is not a manged reference, but rather, a reference in the
    classic C++ sense (it refers to getting the type of "Int32&" for a reference
    type). If you try your code with the type of Object, it returns false as
    well.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
    news:ODA$SfvnDH A.1656@tk2msftn gp13.phx.gbl...[color=blue]
    > Type.HasElement Type -
    > "Gets a value indicating whether the current Type encompasses or refers to
    > another type; that is, whether the current Type is an array, a pointer, or
    > is passed by reference." So why does this return false? String is passed
    > by ref and is a ref type.
    >
    > string s = "text";
    > Console.WriteLi ne("String hasElement:" + s.GetType().Has ElementType);
    >
    > Output:
    > String hasElement:Fals e
    >
    > --
    > William Stacey, MVP
    >
    >
    >[/color]


    Comment

    • William Stacey

      #3
      Re: HasElementType

      "true if the Type is an array, a pointer, or is passed by reference;
      otherwise, false."
      So below, byte[] is passed by ref, as is int[] and they return true as
      expected. Pointer is true as expected. However string is also a ref type
      and passed by ref as is object, so why are they false?

      public static void Main()
      {
      unsafe {
      Console.WriteLi ne("byte[].HasElementType :
      "+typeof(by te[]).HasElementTyp e);
      Console.WriteLi ne("int[].HasElementType : "+typeof(in t[]).HasElementTyp e);
      Console.WriteLi ne("byte*.HasEl ementType: "+typeof(byte*) .HasElementType );
      Console.WriteLi ne("string.HasE lementType:
      "+typeof(string ).HasElementTyp e);
      Console.WriteLi ne("object.HasE lementType:
      "+typeof(object ).HasElementTyp e);
      }
      }

      Output:
      byte[].HasElementType : True
      int[].HasElementType : True
      byte*.HasElemen tType: True
      string.HasEleme ntType: False
      object.HasEleme ntType: False

      --
      William Stacey, MVP

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:#KZQK4vnDH A.744@tk2msftng p13.phx.gbl...[color=blue]
      > William,
      >
      > In the documentation for the HasElementType property, the meaning of
      > "reference" is not a manged reference, but rather, a reference in the
      > classic C++ sense (it refers to getting the type of "Int32&" for a[/color]
      reference[color=blue]
      > type). If you try your code with the type of Object, it returns false as
      > well.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
      > news:ODA$SfvnDH A.1656@tk2msftn gp13.phx.gbl...[color=green]
      > > Type.HasElement Type -
      > > "Gets a value indicating whether the current Type encompasses or refers[/color][/color]
      to[color=blue][color=green]
      > > another type; that is, whether the current Type is an array, a pointer,[/color][/color]
      or[color=blue][color=green]
      > > is passed by reference." So why does this return false? String is[/color][/color]
      passed[color=blue][color=green]
      > > by ref and is a ref type.
      > >
      > > string s = "text";
      > > Console.WriteLi ne("String hasElement:" + s.GetType().Has ElementType);
      > >
      > > Output:
      > > String hasElement:Fals e
      > >
      > > --
      > > William Stacey, MVP
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: HasElementType

        William Stacey <staceywREMOVE@ mvps.org> wrote:[color=blue]
        > "true if the Type is an array, a pointer, or is passed by reference;
        > otherwise, false."
        > So below, byte[] is passed by ref, as is int[] and they return true as
        > expected.[/color]

        No, byte[] references are passed by value, but they fall into the "if
        the Type is an array" clause of the documentation.
        [color=blue]
        > Pointer is true as expected. However string is also a ref type
        > and passed by ref as is object, so why are they false?[/color]

        They're *not* passed by reference.

        See http://www.pobox.com/~skeet/csharp/parameters.html

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • William Stacey

          #5
          Re: HasElementType

          > No, byte[] references are passed by value, but they fall into the "if[color=blue]
          > the Type is an array" clause of the documentation.[/color]

          The "pointer" to the array is passed by value. However it is still a
          reference type. If you use the "ref" keyword to pass it, then your passing
          a pointer to a pointer. And the pointer is passed by value. The
          distinction is, value types are passed by value on the stack, ref types pass
          the 4 bytes pointer by default. It still passing by reference (not to
          confuse that with the "ref" keyword).
          [color=blue][color=green]
          > > Pointer is true as expected. However string is also a ref type
          > > and passed by ref as is object, so why are they false?[/color]
          >
          > They're *not* passed by reference.[/color]

          See above.

          --
          William Stacey, MVP



          Comment

          • William Stacey

            #6
            Re: HasElementType

            Thanks Jon, instead of commenting on all that, here is a document I created
            that gets into the detail of this at the memory level. This shows exactly
            what gets passed when passing a byte[] by-value and by-reference. There is
            also a few other bits that you may find interesting or could answer
            (question is noted in the text.) Please feel free to comment. If you can't
            view this, please also let me know. Cheers!
            http://www.mvptools.com/docs/PassByteArrays.mht

            --
            William Stacey, MVP


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: HasElementType

              William Stacey <staceywREMOVE@ mvps.org> wrote:[color=blue]
              > Thanks Jon, instead of commenting on all that, here is a document I created
              > that gets into the detail of this at the memory level. This shows exactly
              > what gets passed when passing a byte[] by-value and by-reference. There is
              > also a few other bits that you may find interesting or could answer
              > (question is noted in the text.) Please feel free to comment. If you can't
              > view this, please also let me know. Cheers!
              > http://www.mvptools.com/docs/PassByteArrays.mht[/color]

              The problem with looking at it at the implementation level is that
              pass-by-reference and pass-by-value aren't *defined* at the
              implementation level. They're defined in terms of parameters. When you
              talk about the "pointers [to the variables themselves] being passed by
              value" in paragraph 3, that doesn't apply to any definition of pass-by-
              value I've seen, because the pointers aren't the value of any of the
              parameters. The point of describing pass-by-reference and pass-by-value
              semantics is to understand how *parameters* are passed.

              In the case of pass-by-value, the value of the actual parameter
              expression becomes the value of the new local variable (the formal
              parameter).

              In the case of pass-by-reference, the formal parameter is aliased with
              the actual parameter expression (which must therefore be an L-value) so
              that any changes in the formal parameter's value are also visible as
              changes to the actual parameter expression's value.

              The parameters in the call

              GetRefs (ba, ba2, ref ba, ref ba2)

              are ba, ba2, ba (again), and ba2 (again). The first parameters are
              passed by value, the second are passed by reference. There are no other
              parameters, so no other things can really be described as being passed,
              IMO. The title of your page is misleading in this respect - byte arrays
              themselves can never be passed, as there is no expression whose value
              is a byte array - only a *reference* to a byte array.

              One of the reasons the explanation gets difficult (paragraph 6) is that
              the JIT gets involved - and frankly, I don't understand the details of
              what the JIT does. That's the good thing about sticking at the language
              level - everything can be explained by the language specification.
              Obviously we need to have some ideas about performance etc, but in
              terms of explaining the way the language behaves, we can stick at the
              language specification level.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • William Stacey

                #8
                Re: HasElementType

                > value" in paragraph 3, that doesn't apply to any definition of pass-by-[color=blue]
                > value I've seen, because the pointers aren't the value of any of the
                > parameters.[/color]

                The pointers are the only values that get passed in both cases regarding
                reference types. In the (ba, ref ba) example: the first parameter that is
                passed is the reference (i.e. pointer) that ba holds. In the second
                parameter it is the pointer to ba that gets passed (or reference to ba.) In
                both cases, references are passed. That was my point and I think we both
                agree.
                [color=blue]
                > In the case of pass-by-value, the value of the actual parameter
                > expression becomes the value of the new local variable (the formal
                > parameter).[/color]

                Agreed. I don't think I said otherwise. However there is that exception
                where a new local variable address is not created as I showed. I still
                wouldn't mind getting some discussion on that from some kind sole.
                [color=blue]
                > In the case of pass-by-reference, the formal parameter is aliased with
                > the actual parameter expression (which must therefore be an L-value) so
                > that any changes in the formal parameter's value are also visible as
                > changes to the actual parameter expression's value.[/color]

                Agreed. I think I showed that in some detail.
                [color=blue]
                > IMO. The title of your page is misleading in this respect - byte arrays
                > themselves can never be passed, as there is no expression whose value
                > is a byte array - only a *reference* to a byte array.[/color]

                You are welcome to your opinion. As a byte[] is a reference type, it does
                make sense, IMO, to talk about passing reference types by value and by
                reference. In fact, there is a help page on it. There is also one titled
                "Passing Arrays Using ref and out". I think you are parsing a bit fine here
                for some reason. I never said, indirectly or otherwise, that you actually
                pass the bytes of the array itself. If nothing else, that is very clear in
                the text.
                [color=blue]
                > Obviously we need to have some ideas about performance etc, but in
                > terms of explaining the way the language behaves, we can stick at the
                > language specification level.[/color]

                Sure that is fine for many things. Sometimes you need more. It all depends
                on what your talking about or what the question is. Cheers.

                --
                William Stacey, MVP



                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: HasElementType

                  William Stacey <staceywREMOVE@ mvps.org> wrote:[color=blue][color=green]
                  > > value" in paragraph 3, that doesn't apply to any definition of pass-by-
                  > > value I've seen, because the pointers aren't the value of any of the
                  > > parameters.[/color]
                  >
                  > The pointers are the only values that get passed in both cases regarding
                  > reference types. In the (ba, ref ba) example: the first parameter that is
                  > passed is the reference (i.e. pointer) that ba holds. In the second
                  > parameter it is the pointer to ba that gets passed (or reference to ba.) In
                  > both cases, references are passed. That was my point and I think we both
                  > agree.[/color]

                  Sort of - but not quite. The word "reference" here is problematic here
                  because it's so overloaded. In strict ways no reference gets passed in
                  the second case - the *parameter* gets passed *by* reference, and it
                  being a reference-type variable doesn't actually make any conceptual
                  difference. What I'm trying to get at is that the value itself is
                  entirely disregarded (and not even evaluated). Now the fact that it's
                  probably *implemented* as a a pointer which is like a reference
                  shouldn't (IMO) come into these discussions, as that's an
                  implementation detail which should be clearly separated from the
                  language terminology.
                  [color=blue][color=green]
                  > > In the case of pass-by-value, the value of the actual parameter
                  > > expression becomes the value of the new local variable (the formal
                  > > parameter).[/color]
                  >
                  > Agreed. I don't think I said otherwise.[/color]

                  No, I was just clarifying.
                  [color=blue]
                  > However there is that exception
                  > where a new local variable address is not created as I showed. I still
                  > wouldn't mind getting some discussion on that from some kind sole.[/color]

                  But *conceptually* it still *is* being created. I don't care what the
                  implementation actually does so long as the end result is the same as
                  the spec says it will be.
                  [color=blue][color=green]
                  > > In the case of pass-by-reference, the formal parameter is aliased with
                  > > the actual parameter expression (which must therefore be an L-value) so
                  > > that any changes in the formal parameter's value are also visible as
                  > > changes to the actual parameter expression's value.[/color]
                  >
                  > Agreed. I think I showed that in some detail.[/color]

                  Again, just clarifying.
                  [color=blue][color=green]
                  > > IMO. The title of your page is misleading in this respect - byte arrays
                  > > themselves can never be passed, as there is no expression whose value
                  > > is a byte array - only a *reference* to a byte array.[/color]
                  >
                  > You are welcome to your opinion. As a byte[] is a reference type, it does
                  > make sense, IMO, to talk about passing reference types by value and by
                  > reference.[/color]

                  It absolutely makes sense to talk about passing reference types by
                  value and by reference - but passing a byte array reference normally,
                  it is passed by value, rather than the array being passed by reference
                  as you originally claimed.
                  [color=blue]
                  > In fact, there is a help page on it. There is also one titled
                  > "Passing Arrays Using ref and out". I think you are parsing a bit fine here
                  > for some reason. I never said, indirectly or otherwise, that you actually
                  > pass the bytes of the array itself.[/color]

                  No, but you said that byte arrays were passed by reference (implying
                  "by default"), when in fact byte array references are passed by value,
                  which is a different set of semantics.
                  [color=blue]
                  > If nothing else, that is very clear in the text.
                  >[color=green]
                  > > Obviously we need to have some ideas about performance etc, but in
                  > > terms of explaining the way the language behaves, we can stick at the
                  > > language specification level.[/color]
                  >
                  > Sure that is fine for many things. Sometimes you need more. It all depends
                  > on what your talking about or what the question is. Cheers.[/color]

                  But the question, as I understand it, is "are byte arrays passed by
                  reference by default" as that was the thing I disagreed with in your
                  post much earlier on. Determining the answer to that question doesn't
                  require anything beyond the language specification and a definition of
                  what "pass by reference" and "pass by value" each mean.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • William Stacey

                    #10
                    Re: HasElementType

                    Ok. So your saying array are passed by reference?
                    JK :-)

                    --
                    William Stacey, MVP



                    Comment

                    Working...