How can I override ToString() method for all IEnumerable<Int32>?

六眼飞鱼酱① 提交于 2019-12-10 16:16:42

问题


I want to override ToString() on IEnumerable<Int32>.

I was thinking to use Extension methods.

But when I do this below, it still calls the ToString() on System.Object. When I rename my method, then it calls my method.

As my extension method is in a static class, I am not able to override.

How can I achieve this so that my ToString() implementation is called when I call .ToString() on List<Int32> for example?

public static class ExtensionMethods
{
    public static new string ToString(this IEnumerable<Int32> set)
    {
        var sb = new StringBuilder();

        // Do some modifications on sb

        return sb.ToString();
    }
}

回答1:


How can I achieve this so that my ToString() implementation is called when I call .ToString() on List for example?

You can't, basically. Extension methods are only used if no matching instance method can be found.

I suggest you give your method a different name, avoiding the problem - and the potential confusion your method would cause.

Note that even if extension methods were matched in preference to (say) methods declared on object, it would only make a difference for your own code being compiled with an appropriate using directive - not any other code which has already bound the call to the normal one.

If you can give more information about what you're trying to achieve, we may be able to help you more - but for the moment, something like ToDelimitedString (or whatever your method does) sounds like the best bet to me.




回答2:


You cannot replace a method using extension methods.

Method resolution will check for a method belonging to the type, before trying to find matching extension methods.

In other words, you cannot replace ToString, but yes, you can create your own method.

Either create your own IEnumerable<T> type with an overridden ToString method, or use a different method name. Of course, using your own type will of course only work when you're actually using that type.




回答3:


It's not possible to override   ToString, but you can create a wrapper, that you can call in every place, where you are using IEnumerable<Int32>

To output a collection as string I am using an extension method  

 

public static string ToString<T>( this IEnumerable<T> messages)
{ 
     return ToString<T>(messages, Environment.NewLine, "" );
}



ToString<T>( this IEnumerable<T>messages, string separator, string sComment)

is described in my post  ToString function for Generic List

See also similar Overriding ToString() of List<MyClass>

Similar function implemented as an extension method described in post: Separator Delimited ToString for Array, List, Dictionary, Generic IEnumerable




回答4:


You cannot override ToString but you can create a generic method.

This is a simple solution based on Michael Freidgeim's answer (its link is broken):

static public class Extensions {

    // extension for arrays, lists, any Enumerable -> AsString
    public static string AsString<T>(this IEnumerable<T> enumerable) {
        var sb = new StringBuilder();
        int inx = 0;
        foreach (var item in enumerable) {
            sb.Append($"{inx}: {item}\r\n");
            inx++;
        }
        return sb.ToString();
    }
}

Usage:

Console.WriteLine(arr.AsString());
Console.WriteLine(list.AsString());
Console.WriteLine(linqResult.AsString());


来源:https://stackoverflow.com/questions/9697332/how-can-i-override-tostring-method-for-all-ienumerableint32

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