How do I override ToString() and implement generic?

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I have code that I want to make the following changes:

  1. How do I override ToString()? It says: A static member ...ToString(System.Collections.Generic.List)' cannot be marked as override, virtual, or abstract.

  2. 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!

回答1:

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 


回答2:

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);


回答3:

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."



回答4:

You can only override a method if you inherit the base class.

What I would advocate is calling your extension method .ToCsv().



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!