How to pass variables to overridden toString() method?

前端 未结 4 1778
孤独总比滥情好
孤独总比滥情好 2021-01-21 08:43

Is it possible to pass in a bool variable into an overridden toString() method, so it can conditionally print the object in different formats?

4条回答
  •  时光说笑
    2021-01-21 09:43

    The typical pattern for parametrized ToString() is to declare an overload with a string parameter.

    Example:

    class Foo
    {
        public string ToString(string format)
        {
            //change behavior based on format
        }
    }
    

    For a framework example see Guid.ToString

提交回复
热议问题