H5-Interfaces Flashcards
How do you define a interface?
public interface IStudent
{
What is an auto implemented property in an interface?
public class Student : Person, IStudent { // Implement IStudent.Courses. // The student's list of current courses. public List Courses { get; set; }
What is the difference between impliciet and explicit auto implementation of an interface?
If a class implements an interface explicitly, the program cannot access the interface’s members through a class instance. Instead it must use an instance of the interface.
what trick can you use to prevent that you have to implement interface code twice if more classes implement the same interface.
Place the interface implementation in a seperate class and create an instance in the target classes so they can use this for the logic inside the interface methodes.
Which interface do you need to implement to make an items sortable in a list?
IComparable, methode that needs to be implemented is compareTo(object obj)) or as a generic methode
class Car : IComparable { .. public int CompareTo(Car other) {
what alternative can you use to compare objects?
IComparer, implements the Compare(param1, param2) methode.
Give an example of a IComparer implementation.
class CarComparer : IComparer { // The field to compare. public enum CompareField { Name, MaxMph, Horsepower, Price, } public CompareField SortBy = CompareField.Name; public int Compare(Car x, Car y) { switch (SortBy) { case CompareField.Name: return x.Name.CompareTo(y.Name); case CompareField.MaxMph: return x.MaxMph.CompareTo(y.MaxMph); case CompareField.Horsepower: return x.Horsepower.CompareTo(y.Horsepower); case CompareField.Price: return x.Price.CompareTo(y.Price); } return x.Name.CompareTo(y.Name); } }
How do you use external comparer to sort?
Array.Sort(instanceType, comparerInstance);
What interface is used to check if objects are equal?
IEquatable, methode : public bool Equals(Person other)
{…}
Where is the interface that checks if instances are equal used?
SomeCollection.Contains(instanceToCompare)
What interface allows you to clone an object, give an example of an implementation.
class Person : ICloneable { public string FirstName { get; set; } public string LastName { get; set; } public Person Manager { get; set; } // Return a clone of this person. public object Clone() { Person person = new Person(); person.FirstName = FirstName; person.LastName = LastName; person.Manager = Manager; return person; } }
what is a difference between shallow cloning and deep cloning?
In a shallow clone, any reference values in the copy refer to the same objects as
those in the original object. The Person.Clone method class described in this section
is a shallow clone because it sets the clone’s Manager property equal to the
Manager property of the original object.
In a deep clone, the new object’s reference values are set to new objects. The following
code shows how the Person class could provide deep clones:
public object Clone() { Person person = new Person(); person.FirstName = FirstName; person.LastName = LastName; person.Manager = Manager; if (Manager != null) person.Manager = (Person)Manager.Clone(); return person; }
What interface is used with e.g. a foreach loop and what methode is defined in that interface?
IEnumerable, interface method : GetEnumerator
With the enumerator, what type of object do you get back that is used by e.g. foreach and what properties/methodes does this object have?
The return object is of type IEnumerator.
It contains :
property : Current
methodes
* MoveNext
* Reset -> resets the enumerator to just before the beginning of the enumeration.
* Dispose -> cleans up any resources it is using when it is no longer needed.
What is the high level working of the enumerator?
IEnumerable -> getEnumerator -> enumerator object holds properties that let you enumerate through the collection.