Hello everyone, there is the following code:
public abstract class Shape { } public class Circle : Shape { } public interface IContainer<T> { T Figure { get; set; } } public class Container<T> : IContainer<T> { public T Figure { get; set; } public Container(T figure) { this.Figure = figure; } } class Program { static void Main() { Circle circle = new Circle(); IContainer<Circle> container = new Container<Circle>(circle); Console.WriteLine(container.Figure.ToString()); // Delay. Console.ReadKey(); } } So, in short - what I understood in this example.
In this example, we first create an abstract class called Shape {}, then we create a concrete class called Circle — which inherits from the abstract shape class.
then we create an open interface with the name IContainer — parametrized — with a placeholder of type T, and in the body of this interface, we create an abstract auto-property named Figure — such as a placeholder of type T.
Next, vss create a class named Container - which implements the IContainer interface, which in turn (the interface) is parameterized by the type of place to fill with type T.
In the body of the Container class, we implement the abstract property of the Figure, and after that we create a constructor that takes one argument of the placeholder pointer type with type T, and in the body of this constructor, we assign the property of the given constructor argument to our property.
Further, in the Program class, in the body of the Main () method, we create an instance of the Circle class - which, as we remember, is inherited from the abstract Shape class.
On the line:
IContainer<Circle> container = new Container<Circle>(circle); - we create a variable called container — the type of the base interface type IContainer, and the type parameter of the place of filling with the type T of this base interface type we “close” with our type Circle.
Conditionally divide the line below into two parts:
IContainer <Circle> container = new Container <Circle> (circle);
(italics - the first part, fat - the second part)
- as a result, we expect to create an instance of the Container class in the second part of the line — we expect to bring it to the basic Interface type of Icontainer, but the created instance itself will store the property of the type with which we “close” the place of filling with type T when creating this instance - and t . In this case, we "closed" when creating an instance with the Circle type, then in this case the Figure property of the Circle type will be stored inside this instance (or rather, the implementation of this property from a specific Container class inherited from the IContainer interface)
Here is my question: with the abstract class Shape {} and the concrete class Circle {} - inheriting it - everything is clear.
Then, someone explain plainly and humanly what the interface is and why it is needed (no, I certainly studied it, but I didn’t understand it properly; for example, delegates were explained to me in simple language like this - "Delegates are the way to do the following: take an arbitrary piece of code, pack it into an object (delegate instance) and call this piece of code anywhere you can pass a link to our delegate instance. "It may not be entirely correct, but it is understandable!)
With the auto property, its implementation and the designer - everything is also not clear.
it is also not clear why the author of the course says: "but the property will be stored in the created instance itself" - with what fright? Ie as I get it (but I don’t understand for sure) - the instance stores what is passed to it by the constructor (in our case, user-defined) - and since the argument of the constructor takes the value of the set method through the key word, it is cosine (the constructor ) we kind of accept the value of the property in the instance.
And my most important question also arises from "what interfaces are - plainly" - it is unclear why we give the designer a ready-made instance of the Circle class - that is, do we (this instance passed as a constructor argument) lead to the type of this interface.
Finally, here is a picture in which I tried to understand the situation: 