How to create an extension method for ToString?

后端 未结 3 1377
独厮守ぢ
独厮守ぢ 2020-12-01 08:39

I have tried this:

public static class ListHelper
{
    public static string ToString(this IList list)
    {
        return string.Joi         


        
3条回答
  •  旧巷少年郎
    2020-12-01 09:23

    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.

提交回复
热议问题