In addition to Solution 1:
The usual technique used to provide general-purpose string representation of the types you defined (
class
or
struct
) is overriding
System.Object.ToString()
:
Object.ToString Method (System)[
^].
(Yes, such override is possible even for
struct
, despite the fact that such types are
value types; this is how .NET unified type system works.)
Some may say this is the over your head, but better don't think this way. Overriding
Object
methods is a fundamentally important thing. In case of
ToString()
(important: without any parameters), this is a
type-agnostic way to provide a string representation; there are many cases where you can modify the presentation where you cannot do it in a direct way, by just calculation of strings. For example, UI types like
ListBox
or
ComboBox
will show the items based on what this function returns.
See also:
Overriding System.Object.ToString() and Implementing IFormattable | David Hayden[
^].
The best way to calculate some string representing an object with several properties (or, sometimes, other members) is the function
string.Format:
String.Format Method (System)[
^].
—SA