Replies: 5 comments 11 replies
-
For what it's worth, a goal that was mentioned for the ToString method was to be consistent with how ToString has been implemented on anonymous types since C# 3. Console.WriteLine(new { Test = (string)null }.ToString());
Console.WriteLine(new { Test = "" }.ToString()); Prints:
This is not to say that either one or both couldn't be improved. A tough question that arises as soon as you introduce quotes is, what do you do with quotes in the string? How far do you go, do you implement full language-specific escaping in spite of the fact that the .NET class being created by your |
Beta Was this translation helpful? Give feedback.
-
Note that 'escaping' or 'printing' strings in a special way isn't even well defined. What way would that be? C# syntax? VB syntax? Something else? .net printing of strings for these purposes is simple: you take each character and you print it out. If there are no characters (null or empty) you get nothing. |
Beta Was this translation helpful? Give feedback.
-
To me, you real issue has nothing to do with What you use is the most basic form of quality assertion, which relies on the equality defined by the type. And in this case, it is record, in a naïve way. But surely the assert output can be much better: Instead of PS: I'd recommend you to try FluentAssertions. It is quite possible that you won't use MSTest's Assert anymore. |
Beta Was this translation helpful? Give feedback.
-
Oh. Then I definitely don't want to change anything here. I would not want records to behave differently than everything else out there in .net. This is how strings are printed. Anonymous types print things out this way. Console prints it out this way. Etc. Etc. For better or worse, the position since day one seems to be: a null printed string produces no characters. And a normal printed string just prints the contents, ubescaped, without quotes. It's far more important to me that records just follow that then suddenly having them behave differently |
Beta Was this translation helpful? Give feedback.
-
I've seen much crazier than that :-) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Recently I was working on some unit tests using MSTest and
Assert.AreEqual(a, b)
. At the same time, I was trying out using records for my types which seemed like a good fit for simple objects that had a handful of properties and not much else. I knew equality checks were auto-generated which is where I saw the usefulness of records and saw how I don't need to manually assert every property myself, just assert the records are the same viaAssert.AreEqual(a, b)
.While testing my code, I had a few cases that the assertion failed and what it does is it tells me that and calls
ToString
on the object. Unfortunately, the strings it gave back were identical but it still failed the assertion.I actually thought the issue was because I had reference types in records or maybe I was just using records incorrectly. Only by chance did I try running the test with the debugger enabled and carefully examined each property of the records did I realise that two properties (
PreRelease
andBuildMetadata
- both strings) werenull
in one instance and empty strings in the other.Is this a huge issue or something that is insurmountable? No, not really. Could I change how I do testing to avoid tripping up on this issue like testing properties individually? Sure. Do I feel that this is a bit of a trap in the spec? Absolutely, this ambiguity doesn't need to exist.
My thought is simple, just put quotes around string values. If it is empty, it would be empty quotes. If it is null, leave it as-is. This would make my error above look like:
I don't actually know what the full purpose of the
ToString
overrides are for things like records and anonymous types. My assumption is that they are to aide debugging or logging, you wouldn't actually be parsing aToString
to regenerate the object. With that in mind, removing the ambiguity could only help debugging and logging scenarios.Beta Was this translation helpful? Give feedback.
All reactions