3.11. Overriding the Virtual Object Methods

The second issue in the design of a derived class revolves around the three virtual methods that every class implicitly inherits from Object. Recall that by default, ToString() prints out the name of the class. It is generally more useful for a class to display a representation of its internal state. For example, here is the ToString() implementation of our OrQuery class (the lparen and rparen members keep track of any parentheses associated with the query):

 override public string ToString() { StringBuilder sb = new StringBuilder( 8 ); if ( m_lparen != 0 ) gen_lparen( ref sb, m_lparen ); sb.Append( m_lop.ToString() ); sb.Append( " || " ); sb.Append( m_rop.ToString() ); if ( m_rparen != 0 ) gen_rparen( ...

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.