Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding ToString() of List<MyClass>

I have a class MyClass, and I would like to override the method ToString() of instances of List:

class MyClass {     public string Property1 { get; set; }     public int Property2 { get; set; }     /* ... */     public override string ToString()     {         return Property1.ToString() + "-" + Property2.ToString();     } } 

I would like to have the following:

var list = new List<MyClass>             {                 new MyClass { Property1 = "A", Property2 = 1 },                 new MyClass { Property1 = "Z", Property2 = 2 },             };  Console.WriteLine(list.ToString());   /* prints: A-1,Z-2 */ 

Is it possible to do so? Or I would have to subclass List<MyClass> to override the method ToString() in my subclass? Can I solve this problem using extension methods (ie, is it possible to override a method with an extension method)?

Thanks!

like image 615
Bruno Reis Avatar asked Aug 27 '09 10:08

Bruno Reis


People also ask

What happen if we override toString () method?

Override the toString() method in a Java Class A string representation of an object can be obtained using the toString() method in Java. This method is overridden so that the object values can be returned.

Why is it needed to override toString () method in a DTO class?

Overriding toString to be able to print out the object in a readable way when it is later read from the file.

Does Hashmap override toString?

You can't override the symbols in the toString() method directly. Whilst you can use String. replace for maps where the keys and values can't contain = (like Integer s), you'd have to provide a different implementation in general.


1 Answers

Perhaps a bit off-topic, but I use a ToDelimitedString extension method which works for any IEnumerable<T>. You can (optionally) specify the delimiter to use and a delegate to perform a custom string conversion for each element:

// if you've already overridden ToString in your MyClass object... Console.WriteLine(list.ToDelimitedString()); // if you don't have a custom ToString method in your MyClass object... Console.WriteLine(list.ToDelimitedString(x => x.Property1 + "-" + x.Property2));  // ...  public static class MyExtensionMethods {     public static string ToDelimitedString<T>(this IEnumerable<T> source)     {         return source.ToDelimitedString(x => x.ToString(),             CultureInfo.CurrentCulture.TextInfo.ListSeparator);     }      public static string ToDelimitedString<T>(         this IEnumerable<T> source, Func<T, string> converter)     {         return source.ToDelimitedString(converter,             CultureInfo.CurrentCulture.TextInfo.ListSeparator);     }      public static string ToDelimitedString<T>(         this IEnumerable<T> source, string separator)     {         return source.ToDelimitedString(x => x.ToString(), separator);     }      public static string ToDelimitedString<T>(this IEnumerable<T> source,         Func<T, string> converter, string separator)     {         return string.Join(separator, source.Select(converter).ToArray());     } } 
like image 116
LukeH Avatar answered Sep 17 '22 15:09

LukeH