Essentially what I want to do is impliment a class that can contain a list of references to instances of the same type. Something like the following:
interface IAccessibilityFeature
{
List<IAccessibilityFeature> Settings { get; set; }
}
class MyAccess : IAccessibilityFeature
{
List<MyAccess> Settings { get; set; }
}
I know this won't compile because the interface explicitly says my Settings
must be of the type List<IAccessibilityFeature>
. What I am after is some guidance as to the correct way to achieve what I'm trying to do in the MyAccess
class.
Yes, you can define an interface inside a class and it is known as a nested interface. You can't access a nested interface directly; you need to access (implement) the nested interface using the inner class or by using the name of the class holding this nested interface.
Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class. Implement every method defined by the interface.
Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. Interfaces may contain static constructors, fields, constants, or operators. Beginning with C# 11, interface members that aren't fields may be static abstract .
An interface is a set of requirements to which classes must conform if they want to use the service provided by the interface. To implement an interface, first declare that your class will implement the given interface; second, define the methods in the interface.
Try this:
interface IAccessibilityFeature<T> where T : IAccessibilityFeature<T>
{
List<T> Settings { get; set; }
}
class MyAccess : IAccessibilityFeature<MyAccess>
{
List<MyAccess> Settings { get; set; }
}
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