Interfaces Flashcards
WHAT IS AN INTERFACE
- named collection of abstract member definitions which must be implementd by a class or structure that inherits from that interface.
CHARACTERISTICS
- like pure abstract class in that they can contain only the signatures of methods, properties, events or indexers.
- CANNOT contain implementation of methods, fields, properties, events, or indexers and CANNOT directly instantiate like a class.
- can be implemented by both classes and structs
- class or struct implementing an interface MUST implement the members of that interface that are specified in the interface definition.
- interface may inherit from 1 or more interfaces.
- Interfaces may be implemented implicitly (on the class) or explicitly (on the interface). Name clashes from 2 or more interfaces resolved by using an EXPLICIT INTERFACE.
- INterface name begins with capital I ex…IDisposable
Interface Advantages
- Parallel work on different parts of system
- abstract methods functionality used in disparate classes and structs
- Passing interfaces as arguments = flexable. Described by inversion of control and dependency injection patterns.
- allows mock unit testing
- class can inherit one or many interfaces yet one 1 class
- struct can inherit one or many interfaces yet NO inheriting from class or struct
Difference between implicit and explicit interface and when should each be used?
IMPLICIT INTERFACE
- implemented on the class
- derived class has access to interface
- intellisense will show implicit interface methods
EXPLICIT INTERFACE
- implemented on the interface
- used mostly when there is method name clash between two interfaces
Does abstract class that inherits from an interface have to implement ALL the interface’s methods
NO
- abstract class should declare the non-implemented methods as abstract so that derived classes will know they have to implement the specified methods.
- abstract class could also provide a default implementation for the interface methods and mark them as virtual in case they need to be overridden by a concrete class. this technique provides for greater flexability if the method does not have to be implemented for all users.
Can interface inherit from one or more interfaces?
YES
no limit
Many interfaces have generic variants like:
IEquatable and generic IEquatable<t></t>
Should generic be used over non-generic?
Generic is preferred
- avoids inefficient boxing operations on value types.
.NET Framework Built-In Interfaces
What does IEquatable<t> do and when should it be implemented.</t>
- provides a single method that determines whether two instances of a type are equal.
- should be implemented for a struct that will be stored in a generic collection because it it more efficient due to the fact that it avoids boxing.
.NET Framework Built-In Interfaces
What does IEqualityComparer<t> do and when should it be implemented?</t>
- provides a single method that determines whether two instances of a type are equal or not.
- should be used over IEquatable when more than one way is needed to compare two instances of a type.
.NET Framework Built-In Interfaces
What does IComparable<t> do and when should it be implemented?</t>
- provides a single method that a type can implement to order its instances.
- sorting of instances and when the type is used in a generic container like SortedSet<t></t>
.NET Framework Built-In Interfaces
What does IComparer do and when should it be implemented?
- supports ordering when there are multiple ways to order a collection of items.
- MS recommends using the Comparer class be used instead of IComparer interface. this makes comparer implement the non-gneric IComparer interface for free which can be useful when interoping with legacy API’s
.NET Framework Built-In Interfaces
What does IEnumerable<t> do and when should it be implemented?</t>
- exposes an enumerator which can be used to iterate over a collection of a specified type.
- used with LINQ to support in-memory collections.
.NET Framework Built-In Interfaces
What does IQueryable<t> do and when should it be implemented?</t>
- used to query a data source where the type of the data is known. Used with LINQ to support queries on remote data sources.
.NET Framework Built-In Interfaces
Whats does IQueryable do and when should it be implemented?
- this is the non-generic version of IQueryable<t> - used to query datasource where the type of the data is unknown and mostly used with dynamic queries. </t>
- used with LINQ to support queries on a rem ote data source.
.NET Framework Built-In Interfaces
What does IDisposable do and when should it be used
- releases unmanaged resources. Should be used when working with unmanaged objects like C++ dlls.
.NET Framework Built-In Interfaces
WHat does INotifyPropertyChanged do and when should it be used
- notify bindings that a property value has changed. Frequently used in WPF and WinForms projects.