Generalizations and interfaces

Gentlemen, please tell me, this is the case, I read in the book such a phrase about the implementation of generalized interfaces:

class ByTwos<T> : ISeries<T>
Параметр типа Т указывается не только при объявлении класса ByTwos, но и при
объявлении интерфейса ISeries. И это очень важно. Ведь класс, реализующий 
обобщенный вариант интерфейса, сам должен быть обобщенным.

However, next I find this section of code:

class MyClass : IComparable<MyClass>, IEquatable<MyClass>

And most importantly - this code works, why did it say in the beginning that it is necessary to pass its type parameters when connecting a generalized interface?

Thanks.

 1
Author: Виталина, 2014-11-19

1 answers

"Required" - it means that we must specify ISeries<T>, ISeries<int>, ISeries<string> and so on, and not just write ISeries. I agree, the phrase is ambiguous.

About generics:
ISeries<T> - as some call it, it is an open type that has an argument type T, which can be anything if there are no restrictions; and ISeries<int> is already a closed type-a type that has a specific type-argument int.

I'll show you a simple "meaningless" example of how this is used:

Let's say there is generic-interface IMyInterface with one parameter-generalizations T. The interface has one member , which is the MyProperty property of type T:

interface IMyInterface<T>
{
    T MyProperty { get; set; }
}

For example, we were given the task to design a generalized class A, which will implement the interface IMyInterface. We'll do this:

class A<T> : IMyInterface<T>
{
    public T MyProperty { get; set; }
    // остальной код
}

Next, we are given the task of designing an unpublished class B that has the MyProperty property of type int, and that it implemented the IMyInterfaceinterface. We do this:

// ошибка
class B : IMyInterface<T>
{
   public int MyProperty { get; set; }
   // остальной код
}

And we will have an error, because the class B is not shared, it does not know anything about the type T, and at the same time it must implement the MyProperty property of type T.
To complete the task, we must "close" the IMyInterface with a specific type-argument, in our case it is int, i.e.:

class B : IMyInterface<int>
{
   public int MyProperty { get; set; }
   // остальной код
}

What conclusion can be drawn?
When we create a generic class that implements a generic interface, then, of course, in the interface we pass is the type of the class argument, which can be any type (unless, of course, there are restrictions).
If we create an already unpublished class and need to implement a generic interface, then we must pass a specific argument type to this interface, roughly speaking, make a regular one out of the generic interface.

 3
Author: skubarenko, 2014-11-19 17:41:36