How to create an extension method for ToString?

C#.NetTostring

C# Problem Overview


I have tried this:

public static class ListHelper
{
    public static string ToString<T>(this IList<String> list)
    {
        return string.Join(", ", list.ToArray());
    }

    public static string ToString<T>(this String[] array)
    {
        return string.Join(", ", array);
    }
}

But it does not work, both for string[] and List<string>. Maybe I need some special annotations?

C# Solutions


Solution 1 - C#

Extension methods are only checked if there are no applicable candidate methods that match. In the case of a call to ToString() there will always be an applicable candidate method, namely, the ToString() on object. The purpose of extension methods is to extend the set of methods available on a type, not to override existing methods; that's why they're called "extension methods". If you want to override an existing method then you'll have to make an overriding method.

Solution 2 - C#

It sounds like you want to replace what files.ToString() returns. You will not be able to do that without writing a custom class to assign files as (i.e. inherit from List and override ToString().)

First, get rid of the generic type (<T>), you're not using it. Next, you will need to rename the extension method because calling files.ToString()will just call the List's ToString method.

This does what you're looking for.

static class Program
{
    static void Main()
    {
        var list = new List<string> { {"a"}, {"b"}, {"c"} };
        string str = list.ToStringExtended();
    }
}


public static class ListHelper
{
    public static string ToStringExtended(this IList<String> list)
    {
        return string.Join(", ", list.ToArray());
    }
}

Solution 3 - C#

Simply you Shouldn't use the name ToString for the Extension method as it will never be called because that method already exist and you shouldn't use T as its useless there.

For example i tried this and again it returned same thing:

Console.WriteLine(lst.ToString<int>());

output:

shekhar, shekhar, shekhar, shekhar

so this time i used int and it still ran because that T has no use other then changing the Method Prototype.

So simply why are you using ToString Literal as Method name, as it already exist and you can't override it in a Extension method, this is the reason you had to use that T to make it generic. Use some different name like

public static string ToMyString(this IList<String> list)

That way you wouldn't have to use generic as it useless there and you could simply call it as always.


That said your code is working for me. here is what i tried (in LINQPAD):

void Main()
{

	List<string> lst = new List<string>();
	lst.Add("shekhar");
	lst.Add("shekhar");
	lst.Add("shekhar");
	lst.Add("shekhar");
	lst.ToString<string>().Dump();
}

	public static class ListHelper
	{
		public static string ToString<T>(this IList<String> list)
		{
			return string.Join(", ", list.ToArray());
		}
	
		public static string ToString<T>(this String[] array)
		{
			return string.Join(", ", array);
		}
	}

And the output was shekhar, shekhar, shekhar, shekhar

Since you have specified that T in ToString<T> you will need to mention a Type like string or int while calling the ToString method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIAdapterView Question on Stackoverflow
Solution 1 - C#Eric LippertView Answer on Stackoverflow
Solution 2 - C#matt.dolfinView Answer on Stackoverflow
Solution 3 - C#Shekhar_ProView Answer on Stackoverflow