The System.Char dimension

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

    The System.Char dimension

    I've read that the size of char or of the System.Char under .NET is 2
    bytes.

    Then when I do:

    int char_len = Marshal.SizeOf( Type.GetType("S ystem.Char"));

    char_len would be 2!

    Instead the value is 1!

    Where is the error?

  • Christoph Nahr

    #2
    Re: The System.Char dimension

    Read the documentation for Marshal.SizeOf. .. it's explained there.
    --
    Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.

    Comment

    • RicercatoreSbadato

      #3
      Re: The System.Char dimension

      ok, from MSDN: "The size returned is the actually the size of the
      unmanaged type. The unmanaged and managed sizes of an object can
      differ."

      But how can I take the real size of a TYPE ?

      Comment

      • Nick Hounsome

        #4
        Re: The System.Char dimension


        "RicercatoreSba dato" <emailsolidale@ yahoo.it> wrote in message
        news:1138791283 .121962.164780@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > ok, from MSDN: "The size returned is the actually the size of the
        > unmanaged type. The unmanaged and managed sizes of an object can
        > differ."
        >
        > But how can I take the real size of a TYPE ?[/color]

        Why would you want to know?

        You can never access the managed type as a piece of memory so it is never
        useful to know its size.

        The only half-plausible reason is that you want to assess the actual memory
        requirements of your class but even then this is impossible for any
        reasonably complicated class because of stuff like overheads in library
        hashtables which might vary with the data.


        Comment

        • Willy Denoyette [MVP]

          #5
          Re: The System.Char dimension

          Marshal.SizeOf returns the size of the marshaled version of the type.

          What you need is this:
          sizeof(char)

          note however that this is only valid for value types, there is no reliable
          way to obtain the size of reference types.

          Willy.

          "RicercatoreSba dato" <emailsolidale@ yahoo.it> wrote in message
          news:1138783067 .925884.175500@ z14g2000cwz.goo glegroups.com.. .
          | I've read that the size of char or of the System.Char under .NET is 2
          | bytes.
          |
          | Then when I do:
          |
          | int char_len = Marshal.SizeOf( Type.GetType("S ystem.Char"));
          |
          | char_len would be 2!
          |
          | Instead the value is 1!
          |
          | Where is the error?
          |


          Comment

          • RicercatoreSbadato

            #6
            Re: The System.Char dimension

            tnx ;)

            Comment

            Working...