简体   繁体   中英

Is it ok to override ToString() method in this case

I need to be able to retrieve the string version of the object.

For ex: Below SearchCriteria should provide " Search Parameters: FromDate:{0}, ToDate {1}, .. "

public class SearchCriteria
{
  public string FromDate {get;set;}
  public string ToDate {get;set;}
  public string FromAge {get;set;}
  public string ToAge {get;set;}

  //other 10 properties
}

But from other threads I read, it is not recommended to use ToString() for long strings due to performance problems. I would like to know if it is ok to use it this way or shall I consider using private method.

EDIT

This string representation is used in the business logic NOT for debugging purpose.

From the documentation : "Returns a string that represents the current object."

If you want to create a string which is a representation of the object, that is the place.

So - yes!

EDIT:

"This string representation is used in the business logic NOT for debugging purpose."

The string representation of int is also used for business logic. The question is if that string representation is specific for that business logic, or is it just a general representation of that object, which you use in business logic.

If it specific for that specific business logic, the generation of the string suppose to be part of that business logic.

If not, but there are many optional string representations for that object, you may want to implement IFormattable , which defines a ToString method which gets an IFormatProvider and format string as parameters.

Otherwise, the regular ToString is your friend.

Yes, absolutely override ToString() to return whatever is most meaningful information.

I would think that a string that's too long could cause some problems and be less useful. For example, longer strings won't have room in the Watch Window and could have other considerations. So I would look for a way to shorten the output and still be meaningful.

But by all means, override ToString() so your own types return meaningful information.

No.

Of course it's 'ok'. In the sense that pissing your pants is 'ok'. For 5 seconds it's warm and the relieve feels great. But then you need clean pants.

It's not ok in the sense that it closely resembles loose-typing. 'ToString's implementation is so wildly different in objects in the framework in general and more specific in the objects created by all the different tool and business developers.

My 2 cents: Give it a descriptive name, because odds are, later you'll needs yet another string representation of the same object. It's more readable if you develop an API and it's more readable for you later on.

Rather than worrying about performance of ToString() , I would look at readability and expected behavior.

I have always used ToString() to implement the user-friendly display of an object. That is, how would I always want the object to be displayed, even if the caller doesn't know anything about my class (say, writing to a log or on the console, or even displaying in the UI).

For action-specific implementations, I would implement another function, but follow a similar naming pattern. Say, ToFormattedQueryString() .

Here's a good discussion on rules of thumb for ToString() : Why / when would it be appropriate to override ToString? .

If you're concerned about performance, you can add field to your class to store the string, and then add code to set for each property to re-compute your string at the point. Then ToString() can be efficient.

Personally, I think the extra code and the overhead in recomputing that string all the time (which may never be used) will not justify the change.

What I will advise, is that since performance is your primary concern here, go ahead and write the code the way you want to write, in the way that you feel is the more clear and the most maintainable, then let a profiler tool tell you where you need to go back and optimize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM