I have code that I want to make the following changes:
How do I override ToString()? It says: A static member ...ToString(System.Collections.Generic.List)' cannot be marked as override, virtual, or abstract.
How do I make it generic?
public static override string ToString(this List<int> list) { string output = ""; list.ForEach(item => output += item.ToString() + "," ); return output; }
Thanks!
What are you trying to achieve? Often I want to output the contents of a list, so I created the following extension method:
public static string Join(this IEnumerable<string> strings, string seperator) { return string.Join(seperator, strings.ToArray()); }
It is then consumed like this
var output = list.Select(a.ToString()).Join(",");
EDIT: To make it easier to use for non string lists, here is another variation of above
public static String Join<T>(this IEnumerable<T> enumerable, string seperator) { var nullRepresentation = ""; var enumerableAsStrings = enumerable.Select(a => a == null ? nullRepresentation : a.ToString()).ToArray(); return string.Join(seperator, enumerableAsStrings); } public static String Join<T>(this IEnumerable<T> enumerable) { return enumerable.Join(","); }
Now you can consume it like this
int[] list = {1,2,3,4}; Console.WriteLine(list.Join()); // 1,2,3,4 Console.WriteLine(list.Join(", ")); // 1, 2, 3, 4 Console.WriteLine(list.Select(a=>a+".0").Join()); // 1.0, 2.0, 3.0, 4.0
If you want to override ToString()
, you would need to inherit from List<T>
rather than try to extend it. You have already seen that you cannot mark the static extension method as override, and overload resolution will always go for the member method over an extension method if it is available. Your options are
- Inherit and override
- Change your extension method's name to something else
ToSpecialString()
- Call the method directly using the class name
MyExtensions.ToString(myList);
You cannot use extension methods to override an existing method.
From the spec http://msdn.microsoft.com/en-us/library/bb383977.aspx
"You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself."
You can only override a method if you inherit the base class.
What I would advocate is calling your extension method .ToCsv()
.