Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

On a surface level, the CompareTo method in our item simply returns the di erence in their ID numbers, but what does the above do in practice?

Now, when we call Sort() on a List<Item> object, the List will automatically call the Item's CompareTo method when it needs to determine what order to put objects in. Furthermore, besides List<T>, any other objects that need the ability to compare two objects will work with the Item because we have defined the ability for two di erent Items to be compared with one another.

Section 41.5: Implementing multiple interfaces

public interface IAnimal

{

string Name { get; set; }

}

public interface INoiseMaker

{

string MakeNoise();

}

public class Cat : IAnimal, INoiseMaker

{

public Cat()

{

Name = "Cat";

}

public string Name { get; set; }

public string MakeNoise()

{

return "Nyan";

}

}

Section 41.6: Why we use interfaces

An interface is a definition of a contract between the user of the interface and the class that implement it. One way to think of an interface is as a declaration that an object can perform certain functions.

Let's say that we define an interface IShape to represent di erent type of shapes, we expect a shape to have an area, so we will define a method to force the interface implementations to return their area :

public interface IShape

{

double ComputeArea();

}

Let's that we have the following two shapes : a Rectangle and a Circle

public class Rectangle : IShape

{

private double length; private double width;

public Rectangle(double length, double width)

{

this.length = length;

GoalKicker.com – C# Notes for Professionals

172

this.width = width;

}

public double ComputeArea()

{

return length * width;

}

}

public class Circle : IShape

{

private double radius;

public Circle(double radius)

{

this.radius = radius;

}

public double ComputeArea()

{

return Math.Pow(radius, 2.0) * Math.PI;

}

}

Each one of them have its own definition of its area, but both of them are shapes. So it's only logical to see them as IShape in our program :

private static void Main(string[] args)

{

var shapes = new List<IShape>() { new Rectangle(5, 10), new Circle(5) }; ComputeArea(shapes);

Console.ReadKey();

}

private static void ComputeArea(IEnumerable<IShape> shapes)

{

foreach (shape in shapes)

{

Console.WriteLine("Area: {0:N}, shape.ComputeArea());

}

}

//Output:

//Area : 50.00

//Area : 78.54

Section 41.7: "Hiding" members with Explicit Implementation

Don't you hate it when interfaces pollute you class with too many members you don't even care about? Well I got a solution! Explicit Implementations

public interface IMessageService { void OnMessageRecieve();

void SendMessage();

string Result { get; set; } int Encoding { get; set; }

// yadda yadda

}

GoalKicker.com – C# Notes for Professionals

173

Normally you'd implement the class like this.

public class MyObjectWithMessages : IMessageService { public void OnMessageRecieve(){

}

public void SendMessage(){

}

public string Result { get; set; } public int Encoding { get; set; }

}

Every member is public.

var obj = new MyObjectWithMessages();

// why would i want to call this function? obj.OnMessageRecieve();

Answer: I don't. So neither should it be declared public but simply declaring the members as private will make the compiler throw an error

The solution is to use explicit implementation:

public class MyObjectWithMessages : IMessageService{ void IMessageService.OnMessageRecieve() {

}

void IMessageService.SendMessage() {

}

string IMessageService.Result { get; set; } int IMessageService.Encoding { get; set; }

}

So now you have implemented the members as required and they won't expose any members in as public.

var obj = new MyObjectWithMessages();

/* error member does not exist on type MyObjectWithMessages. * We've succesfully made it "private" */ obj.OnMessageRecieve();

If you seriously still want to access the member even though is explicitly implement all you have to do is cast the object to the interface and you good to go.

((IMessageService)obj).OnMessageRecieve();

GoalKicker.com – C# Notes for Professionals

174