System.Tuple(T1,..)
classes marked with [SerializableAttribute]
(see msdn). But they don't have a parameterless constructor so not serializable by XmlSerializer
as it was cleared before at here. So why not add one?
-
3I don't understand why this is being down-voted. It is a rather interesting question to anyone who knows how serialization is supposed to work. – Jonathan Allen Jun 30 '16 at 15:05
-
4Whoever voted to close because it's "primarily opinion based" clearly doesn't understand the question. – Thomas Levesque Jun 30 '16 at 16:07
-
@ThomasLevesque The question is asking people to guess why this framework class was designed the way that it was. That's entirely opinion based. Given that you posted an answer that in no way even attempts to answer the question being asked, namely why this class doesn't have a parameter-less constructor, it seems that you're the one that doesn't understand the quesiton. – Servy Jun 30 '16 at 16:57
-
@Servy, sometimes you have to read between the lines of a question. I think my answer is more useful to the OP than just explaining why there's no public constructor (and it does explain it, although it's indeed just my opinion on the matter). – Thomas Levesque Jun 30 '16 at 19:13
-
@ThomasLevesque So then you see that the question is in fact opinion based, you just wished the OP actually a different question than they actually asked that isn't opinion based, and still feel that it's entirely inappropriate for the opinion based question to be closed for being opinion based. – Servy Jun 30 '16 at 20:17
-
Related: [Why I could not serialize a tuple in C#?](https://stackoverflow.com/questions/13739348/why-i-could-not-serialize-a-tuple-in-c) – dbc Jun 30 '16 at 21:44
-
@Servy you can think whatever you want, I'm not gonna fight over this... – Thomas Levesque Jun 30 '16 at 21:56
-
@ThomasLevesque And yet *you* are the one who said that the question was opinion based, not me, and that you weren't actually trying to answer the quesiton at all, completely contradicting your own statements. So clearly you think the same thing. – Servy Jul 01 '16 at 12:54
-
@Servy, are you done yet? (and for the record, I never said that the question was opinion based... I just quoted the reason given for the close vote) – Thomas Levesque Jul 01 '16 at 14:47
-
@ThomasLevesque You stated that your answer is just your opinion, and not an objectively verifiable answer, so yes, you did in fact say that. – Servy Jul 01 '16 at 14:49
-
@Servy I said that part of my answer was my opinion; the rest of it is purely objective. I really feel like you're just trying to pick up a fight, so this will be my last comment here. As far as I'm concerned, the matter is closed, and I'm not interested in continuing this discussion. – Thomas Levesque Jul 01 '16 at 14:55
-
@ThomasLevesque You said that the part of your answer that was trying to answer the question was just your opinion. The part of your answer that's entirely off topic isn't an opinion, sure. How does that not make the question opinion based? – Servy Jul 01 '16 at 15:08
1 Answers
You don't need a type to have the [Serializable]
attribute to serialize it with XmlSerializer
, and the fact that a type has the attribute doesn't mean you can serialize it with XmlSerializer
; the two are unrelated. [Serializable]
is only used for binary serialization (*) (which doesn't need a parameterless constructor).
To sum it up:
- Binary serialization
- requires the
[Serializable]
attribute - doesn't require a public parameterless constructor
- requires the
- XML serialization
- doesn't require the
[Serializable]
attribute - requires a
public
parameterless constructor
- doesn't require the
So you can't serialize a Tuple<...>
using XmlSerializer
.
Regardless of the above, Tuple<...>
could have a default constructor, but it wouldn't make much sense: tuples are immutable (you can't set their properties), so if you created an instance with the default constructor, the properties would always have their default values and couldn't be changed. (also, the fact that the properties are read-only is also something that prevents tuples from being serialized with XmlSerializer
, which only supports public read-write properties)
(*) actually, not just binary serialization, but any serialization formatter (e.g. BinaryFormatter
, SoapFormatter
...)

- 286,951
- 70
- 623
- 758
-
1This kind of skirts around the question. I believe the spirit of the question is simply - why does Tuple not have a public parameterless constructor so it could be used with XmlSerializer? – bodangly Jun 30 '16 at 16:17
-
@bodangly the OP seems to think that because Tuple is `[Serializable]`, it should have a parameterless constructor and be serializable with `XmlSerializable`, which isn't true – Thomas Levesque Jun 30 '16 at 16:44
-
A `[Serializable]` class can't be serialized that what I stumbled at. Not a thing I'd expect. Yes, `XmlSerializable` needs parameterless constructor. So probably it'd be better named something like `[Formattable]`. – Artyom Jul 02 '16 at 08:27