Structure C# internals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

    Structure C# internals

    I know structure is a value type and derived from System.ValuType . So I have
    the following questions,.

    1 - C# won't allow to derive one structure from other. Then how come it is
    derived from System.ValueTyp e ?

    2 - I have written a simple C# console application. Following is the
    equivalent IL code which is generated for the structure

    ..class sequential ansi sealed nested private beforefieldinit MyStruct
    extends [mscorlib]System.ValueTyp e
    {
    }

    In this it is showing as class. How this is possible ?

    Any help would be useful

    Thanks
  • Jon Skeet [C# MVP]

    #2
    Re: Structure C# internals

    On May 8, 11:41 am, Navaneeth.K.N
    <Navaneet...@di scussions.micro soft.comwrote:
    I know structure is a value type and derived from System.ValuType . So I have
    the following questions,.
    >
    1 - C# won't allow to derive one structure from other. Then how come it is
    derived from System.ValueTyp e ?
    The details of how value types fit into the type hierarchy are
    somewhat murky and involve being *very* precise with terminology. Here
    there's no particular conflict, however: System.ValueTyp e isn't a
    value type itself, it's a reference type - so the "you can't derive
    from value types" rule remains intact.
    2 - I have written a simple C# console application. Following is the
    equivalent IL code which is generated for the structure
    >
    .class sequential ansi sealed nested private beforefieldinit MyStruct
    extends [mscorlib]System.ValueTyp e
    {
    }
    >
    In this it is showing as class. How this is possible ?
    Because ".class" doesn't mean what you think it does (and what most
    people would naturally think it does).
    ".class" is used to declare various kinds of types:

    From ECMA 335:

    <quote>
    [Rationale: For historical reasons, many of the syntactic categories
    used for defining types incorrectly use
    “class” instead of “type” in their name. All classes are types, but
    “types” is a broader term encompassing value
    types, and interfaces as well. end rationale]
    </quote>

    Jon

    Comment

    • Barry Kelly

      #3
      Re: Structure C# internals

      Navaneeth.K.N wrote:
      I know structure is a value type and derived from System.ValuType . So I have
      the following questions,.
      >
      1 - C# won't allow to derive one structure from other. Then how come it is
      derived from System.ValueTyp e ?
      System.ValueTyp e, despite its name, is a reference type. It's basically
      a join type to attach value types into the reference type hierarchy.
      It's treated magically by the CLR, in that descendants of it are value
      types. Similarly, EnumType isn't an enumeration, etc.
      2 - I have written a simple C# console application. Following is the
      equivalent IL code which is generated for the structure
      >
      .class sequential ansi sealed nested private beforefieldinit MyStruct
      extends [mscorlib]System.ValueTyp e
      {
      }
      The '.class' construct in ILASM would be more accurately descriptive if
      it was spelled '.type'. It introduces a new type, not necessarily a
      reference type.

      -- Barry

      --

      Comment

      • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

        #4
        Re: Structure C# internals

        Thanks John, that was an excellent post.

        System.ValueTyp e isn't a
        value type itself, it's a reference type - so the "you can't derive
        from value types" rule remains intact.
        This is making bit confusion. As you know value types are allocated to stack
        (there are exceptional case). So being a reference type, how descendants of
        System.ValueTyp e is getting allocated in stack ? It should go to heap, right ?

        Is it possible to create a custom type which is derived from
        System.ValueTyp e ?

        Thanks again for the reply

        Navaneeth

        Comment

        • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

          #5
          Re: Structure C# internals

          Hi Barry,

          Thanks for the reply. It was helpful.

          Navaneeth

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Structure C# internals

            On May 8, 2:12 pm, Navaneeth.K.N
            <Navaneet...@di scussions.micro soft.comwrote:
            System.ValueTyp e isn't a value type itself, it's a reference type - so the "you can't derive
            from value types" rule remains intact.
            >
            This is making bit confusion. As you know value types are allocated to stack
            (there are exceptional case).
            Well, they're allocated "inline" with whatever their context is:
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            So being a reference type, how descendants of
            System.ValueTyp e is getting allocated in stack ? It should go to heap, right ?
            Basically the CLR knows that types derived from ValueType are value
            types, and does the right thing.
            Is it possible to create a custom type which is derived from
            System.ValueTyp e ?
            Sure, write a struct in C# :)

            I don't believe you can create your own reference type deriving from
            ValueType.

            Jon

            Comment

            • =?Utf-8?B?TmF2YW5lZXRoLksuTg==?=

              #7
              Re: Structure C# internals

              Hi

              Thanks again for the reply. I will be glad if you can look into the
              following point.

              1 - As per this
              (http://msdn.microsoft.com/hi-in/libr...us,VS.80).aspx) MSDN
              article, .NET having two types of value-types. Built in and user-defined.
              They say user defined types are derived from "System.ValueTy pe". But what
              about int32, boolean etc ? Is that also derived from "System.ValueTy pe" ?

              2 - IL allows deriving from System.ValueTyp e. But why C# is restricting us
              to derive from that ? any ideas ?

              Thanks again

              Navaneeth

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Structure C# internals

                On May 9, 6:25 am, Navaneeth.K.N
                <Navaneet...@di scussions.micro soft.comwrote:
                Thanks again for the reply. I will be glad if you can look into the
                following point.
                >
                1 - As per this
                (http://msdn.microsoft.com/hi-in/libr...us,VS.80).aspx) MSDN
                article, .NET having two types of value-types. Built in and user-defined.
                They say user defined types are derived from "System.ValueTy pe". But what
                about int32, boolean etc ? Is that also derived from "System.ValueTy pe" ?
                Yes. Take a look at the docs for System.Int32 etc.
                2 - IL allows deriving from System.ValueTyp e. But why C# is restricting us
                to derive from that ? any ideas ?
                IL doesn't let you derive a reference type from System.ValueTyp e, only
                a value type - and C# lets you do that too, by declaring a struct
                instead of a class.

                Jon

                Comment

                Working...