Override .ToString method c#

后端 未结 7 2101
北海茫月
北海茫月 2020-11-27 07:52

Okay, so I wrote this program out of the exercise of a C# programming book (I\'m trying to learn here) and it asks for \"Override the ToString() method to return all

7条回答
  •  无人及你
    2020-11-27 07:57

    If you are using C# 6 (or later) use the nameof() method for the property names in the string in case the property names change. You can also use the $"" notation instead of using string.Format().

    For example:

    public override string ToString()
    {
        return $"{nameof(Name)}: {_name}";
    }
    

提交回复
热议问题