How do I override ToString() and implement generic

2019-02-20 18:06发布

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!

4条回答
你好瞎i
2楼-- · 2019-02-20 18:09

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

查看更多
Lonely孤独者°
3楼-- · 2019-02-20 18:10

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);
查看更多
forever°为你锁心
4楼-- · 2019-02-20 18:17

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

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

查看更多
地球回转人心会变
5楼-- · 2019-02-20 18:24

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
查看更多
登录 后发表回答