XmlSerializers, Structs, and Default Constructors

F

Flinky Wisty Pomm

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?
 
B

bruce barker \(sqlwork.com\)

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)
 
F

Flinky Wisty Pomm

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top