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

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

I thought to use extension methods.

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

Since my extension method is in a static class, I cannot override it.

How can I achieve this so that my ToString() implementation is called when I call .ToString() on a 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(); } } 
+4
source share
4 answers

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

You cannot, in principle. Extension methods are used only if no suitable instance method is found.

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

Please note that even if extension methods were mapped in preference (say) to methods declared in object , this will only matter for your own code compiled with the corresponding using directive, and not with any other code that has already connected the call to ordinary.

If you can give more information about what you are trying to achieve, we can help you more, but for now, something like ToDelimitedString (or whatever your method does) sounds like the best choice for me.

+9
source

You cannot replace a method using extension methods.

Method permission will check the method belonging to the type before trying to find the appropriate extension methods.

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

Create your own IEnumerable<T> type using the overridden ToString method or use a different method name. Of course, using your own type, of course, will only work when you really use this type.

+3
source

It is not possible to override ToString, but you can create a wrapper that you can call anywhere you use IEnumerable<Int32>

To output the collection as a string, I use the 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) 

described in my post ToString function for a general list

See also a similar ToString () Override from the <MyClass> list

A similar function is implemented as the extension method described in the message: Separator separated by ToString for array, list, dictionary, general IEnumerable

0
source

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

This is a simple solution based on Michael Freidheim's answer (his link does not work):

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

Using:

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

Source: https://habr.com/ru/post/1401314/


All Articles