Record classes dont inherit ToString method from base. #109484
-
I am creating a node structure in XML for the XMPP protocol. To simplify the code structure regarding cloning XML elements to modify without changing the original. I used record classes and inheritance between record and the subtypes of elements. However, the base record class that implements ToString generates the XML without indentation, but the classes inherited from records do not inherit the ToString from the record base class. Is this behavior really intentional? Because I really like using the public override string ToString() => ToString(false);
public string ToString(bool indented)
{
var sb = new StringBuilder();
var formatting = XmlFormatting.Default;
if (indented)
formatting |= XmlFormatting.Indented;
using (var sw = new StringWriter(sb))
WriteTo(sw, formatting);
return sb.ToString();
} To get correct XML string representation of my Element class, i must override ToString again for each record class that inherits from my Element base class. public sealed record StreamFeatures : Element
{
public override string ToString() => base.ToString(false); // without override this method, it prints original record ToString instead Element::ToString()
public StreamFeatures(StreamFeatures other) : base(other)
{
}
public StreamFeatures() : base("stream:features", Namespaces.Stream)
{
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
See dotnet/sdk#43617 .
Note that there isn't one implementation of "record ToString". Every record generates its own ToString with that semantic. |
Beta Was this translation helpful? Give feedback.
See dotnet/sdk#43617 .
Record class always generates a new override for
ToString
, which uses the default record semantics and doesn't usesbase.ToString
.Note that there isn't one implementation of "record ToString". Every record generates its own ToString with that semantic.