Interfaces Flashcards

1
Q

WHAT IS AN INTERFACE

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Interface Advantages

A
  1. Parallel work on different parts of system
  2. abstract methods functionality used in disparate classes and structs
  3. Passing interfaces as arguments = flexable. Described by inversion of control and dependency injection patterns.
  4. allows mock unit testing
  5. class can inherit one or many interfaces yet one 1 class
  6. struct can inherit one or many interfaces yet NO inheriting from class or struct
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Difference between implicit and explicit interface and when should each be used?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Does abstract class that inherits from an interface have to implement ALL the interface’s methods

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can interface inherit from one or more interfaces?

A

YES

no limit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Many interfaces have generic variants like:

IEquatable and generic IEquatable<t></t>

Should generic be used over non-generic?

A

Generic is preferred

  • avoids inefficient boxing operations on value types.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

.NET Framework Built-In Interfaces

What does IEquatable<t> do and when should it be implemented.</t>

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

.NET Framework Built-In Interfaces

What does IEqualityComparer<t> do and when should it be implemented?</t>

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

.NET Framework Built-In Interfaces

What does IComparable<t> do and when should it be implemented?</t>

A
  • 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>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

.NET Framework Built-In Interfaces

What does IComparer do and when should it be implemented?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

.NET Framework Built-In Interfaces

What does IEnumerable<t> do and when should it be implemented?</t>

A
  • exposes an enumerator which can be used to iterate over a collection of a specified type.
  • used with LINQ to support in-memory collections.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

.NET Framework Built-In Interfaces

What does IQueryable<t> do and when should it be implemented?</t>

A
  • used to query a data source where the type of the data is known. Used with LINQ to support queries on remote data sources.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

.NET Framework Built-In Interfaces

Whats does IQueryable do and when should it be implemented?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

.NET Framework Built-In Interfaces

What does IDisposable do and when should it be used

A
  • releases unmanaged resources. Should be used when working with unmanaged objects like C++ dlls.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

.NET Framework Built-In Interfaces

WHat does INotifyPropertyChanged do and when should it be used

A
  • notify bindings that a property value has changed. Frequently used in WPF and WinForms projects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

.NET Framework Built-In Interfaces

What does ISerializable do and when should it be implemented?

A
  • provides way for object to serialize itsself to a binary stream of bytes and deserialize that stream of bytes back to an object.
17
Q

What is difference between interface and abstract class and when to choose which one?

A
  1. Interface can only contain signature.
  2. Interface can contain signatures of methods, propertties, events, and indexers. Abstract class can in addition also implement code for fields, constructors, finializers, delegates, operators, enums, as well as other types of classes and structs.
  3. Interface permits multiple inheritance
  4. Interface not permitted access modifiers since all implicitly public. Abstract class may specify public or internal.
  5. Interface defines the peripheral abilities of a class. Abstract class defines the core identity of a class and how it can be used.
  6. INterface should be used if the methods are likely to be used by unrelated classes.
  7. If a new method is added to an interface - then every class that unses that interface will need to be updated in order to implement it. Abstract class can be given a default implementation as a virtual method. - which would not require updating all of the classes when the new update is released.
  8. Interface can only inherit from one or more interfaces where as an abstract class can inherit from multiple interfaces as well as a single abstract or concrete class.
18
Q

when defining a method that takes a type like class or struct as an argument, should the method ever be altered to take an interface instead?

A

YES

  • when method focuses on the methods supported by interface and does not use any method of the type outside of that interface, then the method should be defined to take an interface as the argument instead.
  • methods taking conrete type as an argument leads to brittle code.
  • testing easier this way too
19
Q

Can an interface define fields and implement methods?

A

NO

  • interfaces can only contain signatures of methods, properties, events or indexers.
  • cannot contain implementation and cannot directly instantiate like a class.
20
Q

Interface can contain signatures of one or more methods. Are there any other kinds of signatures that can be defined for other types of interface members. Is so what are they?

A

Interfaces can contain signatures for:

  1. methods
  2. properties
  3. indexers
  4. events
21
Q

Is there a way for a class to inherit from an interface withouth defining an implementation for the interface’s methods?

A

YES

  • by declaring class as abstract. the abstract class shuld declare the non-implemented methods as abstract so that derived classes will know they have to implement them.
  • 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 if there are some methods that dont need to be implemented by a user.
22
Q

If IEquatable<t> is implemented for a type, then should any other methods be overriden?</t>

A

YES

  • the Equals and GetHashCode methods should be overridden for that type asd well in order to keep the behavior consistent.
23
Q

.NET Framework Built-In Interfaces

if a collection of custom class objects inside a container will be sorted at some point, then what interface should the custom type implement to help accomplish this?

A

the IComparable or IComparable<t> interface should be implemented to order or sort a type's instances. The CompareTo method is the sole method that needs to be implemented. </t>

24
Q

.NET Framework Built-In Interfaces

If a collection of structs inside a container will be sorted at some point, then what interface should the struct implement toi accomplish this most efficiently?

A

The IComparable<t> interface should be used instead of IComparable </t>

  • the IComparable<t> interface does not use any boxing and is thus more efficient.</t>
  • IComparable.CompareTO method requires an object parameter which is boxed and trhen unboxed.
25
Q

If implementing IComparable<t> to support sorting operations for a given type, should anything else also be implemented?</t>

A

the non-generic version of IComparable should be implemented because it is used in the Array.Sort method. Atype released to the outside world should support sorting in generic types as well as arrays.

In addition, IEquatable<t> should also be implemented as well as the &gt; &gt;= &lt; and &lt;+ operators to return values that are consistent with CompareTO. </t>

26
Q

If a collection of custom objects inside a container need to be sorted using different criteria at different points, then what interface should be used for this purpose?

A
  • define a new class that implements the IComparer<t> interface. Most of the .NET sort methods provide an overload to accept an instance of a class that implements the IComparer interface. </t>
27
Q

How can a struct efficiently define equality without boxing?

A

implement IEquatable<t> interrface </t>

28
Q

How can class or struct provide different ways to determine value equality?

A

for each new kind of equality that needs to be implemented a new class based off of the EqualityComparer class should be implemented.

29
Q

How can a custom collection provide a way to enumerate the collection with the foreach statement?

A

collection should implement the IEnumerable<t> interface</t>

30
Q

What interface should be used to query a remote data source where the type of the data is known?

A

IQueryable<t> interface is used to query a data source where the type of data is known. </t>

  • used with LINQ
31
Q

Whate interface should be used to query a remote data source where the type of the data is NOT known

A

IQueryable interface when data source type is NOT KNOWN

  • used with LINQ
32
Q

An APP is uning an unmanaged resource. Whatt pattern or interface should be implemented to clean up the resource when it is no longer needed. What are the details of its use.

A

IDisposable pattern / interface cleans up unmanaged resources

  • caller can directly clean up the unmanaged resource by calling the Dispose method. pattern also inclides a way to handel cleaning up the unmanaged resource if the caller fails to call the dispose method.
  • pattern is implemented by overridding the Object.FInalize method to call the Dispose method. The finalize method is called by the garbage collector right before it collects the managed resource.
  • a boolean flag is used to determine is the Dispose method has already been called directly by the class user. If, so then the method does not call the Dispoase method again.
  • the finalize method looks very much like C++ destructor but operates differently by not being called when right out of scope. finalize method is called by the garbage collector some time later on a different thread when it goes to collect the object.
33
Q

what interface should be implemented in order to notify a client when a property has changed?

A

INotifyPropertyChanged interface is used to notife binding clients that a property value has changed. this interface is frequently used in WPF and WinForms

34
Q

What interface should be implemented to convert an object into a binary strea of bytes as well as convert the stream of bytes backj into the original object

A

ISerializable interface provides a way for an object to serialize itself to a binary stream of bytes and deserialize the stream of bytes back into the original object.

35
Q

should an interface be written for every public class that is written

A
  • Some groups this is the norm.
  • other groups say interfaces only when:
  1. test supports - when Q/A needs to mock the stub classes
  2. implementation abstraction - using dependency injection / inversion of control
  3. recommend interface only if you need to break a dependency, or want multiple independent implementations.
36
Q

The following interface is defined: interface ICage<t> where T : IAnimal { void Enclose(T animal, int size); } Is it possible to define the Enclose method within a new class that inherits from ICage (without modifying the interface) such that the age parameter does not have to be specified by its callers?</t>

A

Yes, this can be accomplished by defining the size parameter to be an optional parameter within the class’s method definition. For example:

class Cage<t> : ICage<t> where T : IAnimal</t></t>

{ public void Enclose(T animal, int size = 10) {

} }

When it comes to implementing an interface method, optional parameters have no effect on the method signature.

A method defined in an interface that uses a default parameter may be implemented in a class without specifying it as a default parameter. Likewise, A method defined in an interface that does not use default parameters may be implemented in a class to use default parameters.