Elegant solution to class design-Collection of common programming errors

It should be possible with extension methods by using generics. Multiple approaches are possible, but this one is simplest. Although you do get that if

public static void Foo(this T objectC)
     where T: C
{
     if(typeof(T)==typeof(B){ //or for runtime check:     if(objectC is B)
          //specific
     }
}

you could then call Foo on any instance of A or B.

You mention you don´t want if statements, but I’m not sure to which extend you’re trying to avoid that? The only way to completely avoid it, is to have 2 extension methods, one for A and one for B, (which in turn can call a common method for C), but I think you’re trying to avoid multiple extension methods?

edit If you absolutely want to prevent if’s, you’ll have to use multiple extension methods as shown in Frederik’s post. You could add an extension for the baseclass as well, that only gets called if the type is not known during compilation. But that would still need an if 😉

public static void Foo(this A a)
{
}

public static void Foo(this B b)
{
}

public static void Foo(this C c)
{
    if(c is A)
       Foo((A)c);
    else if(c is B)
       Foo((B)c);
    else
       throw new NotSupportedException(c.GetType().FullName);
}

If the type is always known at compile time, you can simply use the 2 extension methods for A en B.