XmlSerializers, Structs, and Default Constructors

Discussion in 'ASP .Net' started by Flinky Wisty Pomm, Jun 19, 2006.

  1. Hi, this is just out of curiosity really. I had a struct, requirements
    changed and I needed to inherit from it, so I made it into a class.

    After the change, XmlSerializer choked because XmlSerializable classes
    require a default constructor. Why don't structs?
    Flinky Wisty Pomm, Jun 19, 2006
    #1
    1. Advertising

  2. structs are not objects, but rather a value types. they are allocated on the
    stack, rather than the heap. to improve value type performance, they do not
    have contructors like objects.

    -- bruce (sqlwork.com)

    "Flinky Wisty Pomm" <> wrote in message
    news:...
    > Hi, this is just out of curiosity really. I had a struct, requirements
    > changed and I needed to inherit from it, so I made it into a class.
    >
    > After the change, XmlSerializer choked because XmlSerializable classes
    > require a default constructor. Why don't structs?
    >
    bruce barker \(sqlwork.com\), Jun 19, 2006
    #2
    1. Advertising

  3. Structs DO have constructors, but I found an answer with a quick
    Google. In case anyone else is sad enough to care, structs have an
    explicit but hidden default constructor whose purpose is to assign
    default values to field members.

    That's why you can't create an explicit default constructor on a
    struct, the .Net framework creates one under the covers. This default
    constructor is presumably used by the XmlSerializer.

    Mystery solved.


    bruce barker (sqlwork.com) wrote:
    > structs are not objects, but rather a value types. they are allocated on the
    > stack, rather than the heap. to improve value type performance, they do not
    > have contructors like objects.
    >
    > -- bruce (sqlwork.com)
    >
    > "Flinky Wisty Pomm" <> wrote in message
    > news:...
    > > Hi, this is just out of curiosity really. I had a struct, requirements
    > > changed and I needed to inherit from it, so I made it into a class.
    > >
    > > After the change, XmlSerializer choked because XmlSerializable classes
    > > require a default constructor. Why don't structs?
    > >
    Flinky Wisty Pomm, Jun 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,246
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    572
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    582
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    381
    Juha Nieminen
    Sep 19, 2008
  5. XmlSerializers.dll and faster startup

    , Oct 23, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    157
    John Saunders
    Oct 24, 2006
Loading...

Share This Page