简体   繁体   中英

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

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

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM