Is it possible to make extensions in a non-static class? [closed]

i need the extension in is a public class and not a public static class

So it’s not about the class being public, it’s about it being static or not. No, extension methods have to be in a static, non-generic, top-level class.

But your sample method doesn’t use any instance members, so can be static, and can be in a static class. I’d rename the Client parameter to client to follow .NET naming conventions though – and I think it makes more sense for class to be called control, given the constraint. So your code would end up as:

internal static T SetClient<T>(this T control, DiscordClient client) where T : Control
{
    // Equivalent to control != null, but preferred for modern C# code.
    if (control is object)
    {
        control.Client = client;
    }
    return control;
}

Browse More Popular Posts

Leave a Comment