Duplicate of about 20 recent questions; someone please link them.
For simple scenarios when should i use one or the other? What are the pros & cons?
What are the recommendations for using extension methods?
EDIT:
Let me give an example. Lets say i have a web relative path represented as a string. Now i want to write a method which 1) Checks if the path begins with '/en/ or '/fr/' 2) if it does not then take the value to prepend from another string.
eg Extension Method
public static string ToLocaleRelativePath(this string s, string contextPath)
{
//1. Check if string begins with a locale
//2. If it does not prepend the value extracted from the context path
}
Is such an operation suited for an exteniosn method or should it be a utility?
Thanks
In your example, a utility class is probably more appropriate, because it makes sense only for strings representing relative URLs, rather than for all strings. By contrast, a method that applied to all (or the huge majority of) strings (say, a Trim or Reverse method (if such things didn't already exist)) would fit nicely as an extension method.
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.