Extension Method for Generic Class [duplicate]

扶醉桌前 提交于 2019-12-04 00:09:33
Stan R.

To extend any class

public static class Extensions
{
    public static T DoSomething<T>(this T obj)
    {
        //...
    }
}

To extend a specific generic class

public static NeedExtension<T> DoSomething<T>(this NeedExtension<T> obj)
{
    //...
}

Yes, but you forgot the this keyword. Look at Queryable that provides all the LINQ operators on collections.

Sure

public static void SomeMethod<T>(this NeedsExtension<T> value) {
  ...
}
Asad

How do you write a C# Extension Method for a Generically Typed Class

public static class NeedsExtension<T>
{
    public static string DoSomething <T>(this MyType<T> v)
    {   return ""; }

    // OR
    public static void DoSomething <T>(this MyType<T> v)
    {   
         //...
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!