Introduction:
A generic interface is nothing but an interface with a generic Type T, since in interfaces we know that a class which inherits an interface must implement all the methods of that particular interface.
A generic Interface can be of Type T, where as T can be refereed to anything, it can be of any data type or even a class.
we declare a generic Interface like this
Public Interface myInterface<T>
Interface is to be implemented by a class, and Interface of Type 'T' needs to match the corresponding Type inside that particular class, when any of the method is to be called by an object of that class.
In this example how the concept of generic Interface works in c#, we will illustrate an example of saving and then getting back a records of a Student.
Step 1: So first we will create a Project in Visual studio 2017 and we will name it as GenericInterfaceExample and click OK.
Step 2:
Now, we will create a generic Interface, we have named it as IGenericInterface, and we will create a class which will implement that Interface.
Here we have initialized three methods in this interface
The first method is to the add the students record into the list, the 2nd is to count the total number of students and the 3rd method is to Get all the students data at a particular index or position.
Now lets create a new class for inheriting this interface, we will name this class as generics, which is going to implement all the methods present in the interface it inherits.
So we have a generic class and we have called it as generics<T> and it implements the above interface called IGenericInterface
In the above code, we have a generic class generics<T> which implements the interface, then a we have a generic array of max size of 10.
We have implemented the data members and the methods from the generic interface, So the add method adds a particular student record at a specific location,
Count returns the total number of rows, or the total number of student records, while Get which returns a generic type gets the of a particular student at any index or position.
Step 3:
In this step we will create a class called Student, we will define different properties for student class, so that we can and set and get the student record.
Step 4:
In this step we will create an instance of the generic interface type and add records for students, we will add students using studentData object, which we initialize for the generic interface of type Student, so here we will pass the type T as Student to the generic interface.
So in the above code, at line 67, we will initialize a for loop and we will get the total number of students by calling the Count data member.
At line 69 we will get the student record at some specific location or index, so for example at 1st iteration, the index will be 0 and so on, Get is the method we initialized in the generic interface.
At line 71, 72 and 73, the student class object will call the properties of the the student class for each and every iteration.
Press crtl+f5 to run the program and see the output window as,
Very well Explained...
ReplyDelete