Only generic classes can implement generic interfaces. Normal classes can't implement generic interfaces.
You cannot inherit a generic type. // class Derived20 : T {}// NO!
Interface Type Constraint You can constrain the generic type by interface, thereby allowing only classes that implement that interface or classes that inherit from classes that implement the interface as the type parameter.
Declaring those constraints means you can use the operations and method calls of the constraining type. If your generic class or method uses any operation on the generic members beyond simple assignment or calling any methods not supported by System. Object, you'll apply constraints to the type parameter.
You include the entire signature of your class before you define generic constraints.
class DerivedFoo<T1, T2> : ParentFoo<T1, T2>, IFoo where T2 : IBar
{
...
}
My recommendation: when you have a question about the syntax of the C# language, read the specification; that's why we publish it. You'll want to read section 10.1.
To answer your specific question, the order of things in a class declaration is:
Everything on that list is optional except for "class", the name, and the body, but everything must appear in that order if it appears.
public interface IFoo {}
public interface IBar {}
public class ParentFoo<T,T1> { }
public class DerivedFoo<T, T1> : ParentFoo<T, T1>, IFoo where T1 : IBar { }
public class KeyAndValue<T>
{
public string Key { get; set; }
public virtual T Value { get; set; }
}
public class KeyAndValue : KeyAndValue<string>
{
public override string Value { get; set; }
}
This is an extension off the existing answers. It defaults to string
if you don't supply a type. I didn't implement an interface but that shouldn't require anything different than usual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With