dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.27k stars 5.9k forks source link

Example with object expressions with IFormattable does not work as intended and gives a wrong example #9707

Closed abelbraaksma closed 5 years ago

abelbraaksma commented 5 years ago

The example code with the object expression for IFormattable doesn't work, for instance, in FSI, it prints the following:

> "{FSI_0002+Delimiter@1}"

The reason this happens is the wrong assumption that the this-qualifier holds anything useful (like a string or a number) but since object expressions aren't assignable, as written it is a poor example of showcasing the usefulness.

In fact, I think that a note on the what the this-qualifier points to in such cases would be useful. As far as I know, it is only somewhat useful when you use a base class, instead of an interface, as with a base class at least it points to its instance.

While at the subject, perhaps we can improve the examples here a bit? For instance with a few real-world use-cases like implementing IComparable on the fly, or a combination of both interfaces and class. Also, an example show-casing using the generic-type-params from the syntax is missing.

I could come up with some examples if this is considered useful.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

cartermp commented 5 years ago

Issue fixed in #10392 - @abelbraaksma if you have an idea for a different sample, we'd definitely love the PR.

abelbraaksma commented 5 years ago

@cartermp, I worked a bit with the idea I posted, i.e. the IComparable on the fly. But I couldn't make a trivial worked out example. I'll give it another go, I might not have understood this syntax enough back then