H5-Interfaces Flashcards

1
Q

How do you define a interface?

A

public interface IStudent

{

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

What is an auto implemented property in an interface?

A
public class Student : Person, IStudent
{
// Implement IStudent.Courses.
// The student's list of current courses.
public List Courses { get; set; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between impliciet and explicit auto implementation of an interface?

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

what trick can you use to prevent that you have to implement interface code twice if more classes implement the same interface.

A

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.

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

Which interface do you need to implement to make an items sortable in a list?

A

IComparable, methode that needs to be implemented is compareTo(object obj)) or as a generic methode

class Car : IComparable
{
..
        public int CompareTo(Car other)
         {
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what alternative can you use to compare objects?

A

IComparer, implements the Compare(param1, param2) methode.

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

Give an example of a IComparer implementation.

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

How do you use external comparer to sort?

A

Array.Sort(instanceType, comparerInstance);

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

What interface is used to check if objects are equal?

A

IEquatable, methode : public bool Equals(Person other)

{…}

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

Where is the interface that checks if instances are equal used?

A

SomeCollection.Contains(instanceToCompare)

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

What interface allows you to clone an object, give an example of an implementation.

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

what is a difference between shallow cloning and deep cloning?

A

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

What interface is used with e.g. a foreach loop and what methode is defined in that interface?

A

IEnumerable, interface method : GetEnumerator

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

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?

A

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.

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

What is the high level working of the enumerator?

A

IEnumerable -> getEnumerator -> enumerator object holds properties that let you enumerate through the collection.

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

What 2 ways are there to ensure that an object does not lock a resource when the reference to that object is set to null?

A
  • Create a Descructor

- Implement the IDisposable interface.

17
Q

How do you define a destructor and what are the properties of a descructor?

A
Destructors can be defined in classes only, not structures.
A class can have at most one destructor.
Destructors cannot be inherited or overloaded.
Destructors cannot be called directly.
Destructors cannot have modifiers or parameters.
18
Q

What is the difference between a Destructor and the Dispose methode?

A

In the Dispose methode you can free up managed and unmanaged resources. In a descructor you can not remove managed resources.

19
Q

To wat does the destructor gets converted and what can you do to make an application more efficient?

A

To a finalizer methode.

GC.SuppressFinalize(this); -> takes the object out of the finalizer queue

20
Q

Which statement automaicly calls Dispose, give an example.

A
The using(...) statement. 
Example using(MyType myRef = new MyType())
{

}