Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an extension method for ToString?

Tags:

c#

.net

tostring

People also ask

How do I extend my toString?

Override the toString() method in a Java Class A string representation of an object can be obtained using the toString() method in Java. This method is overridden so that the object values can be returned.

What is extension method with example?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

Why should you override the toString () method?

When you create a custom class or struct, you should override the ToString method in order to provide information about your type to client code. For information about how to use format strings and other types of custom formatting with the ToString method, see Formatting Types.


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.


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

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.