Introduction The interfaces in C # provide a polymorphic implementation of runtime. An interface can use a reference to the same interface to access methods that implement different classes of the same interface, actually using virtual methods to invoke different classes of the same basis through the same reference. Use the simple short class example to explain the concept of the interface before you begin, and the short example below shows what the interface looks like.
P1.cs
Program code:
Class Demo {public static void Main () {System.Console.WriteLine ("Hello Interfaces"); } }
Interface ABC {}
Output:
Compile and run the program above to run the program and display the desired results. This program contains a demo class program getting Started in the main () method to print "Hello Interfaces". The interface ABC is also defined in the above program. The ABC interface is empty, and some elements can be added to the interface.
P2.cs
Program code:
Class Demo {public static void Main () {System.Console.WriteLine ("Hello Interfaces"); } }
Interface ABC {int x; }
Output:
| P2.cs (11,3): Error cs0525:interfaces cannot contain fields |
error! You cannot include fields such as variables in the interface of C #. The above program declares an integer variable x in interface ABC. Compilation will be an error.
p3.cs
program code:
class Demo { public static void Main () { system.console.writeline ("Hello Interfaces"); }
interface ABC { void xyz () { system.console.writeline ("in XYZ");    } }
output:
| P3.cs (11,8): Error CS0531: ' abc.xyz () ': interface members cannot has a definition |
this time the XYZ () method was defined in the interface and the C # compiler found an error. This means that members cannot have definitions in the interface. It also means that if only the method declaration compiler in interface ABC is considered correct?
P4.cs
Program code:
Class Demo {public static void Main () {System.Console.WriteLine ("Hello Interfaces"); } }
Interface ABC {void XYZ (); }
Output:
The above program compiles correctly to produce the desired output. The final compilation was successful. Only the definition of the method is included in the interface of C #. Now look at the role of the method.
An interface is a specification of a class implementation. This means that the interface defines the prototype of the method and has the class to implement the method prototype defined by the interface.
So the class demo and interface ABC are combined together.
P5.cs
Program code:
Class Demo:abc {public static void Main () {System.Console.WriteLine ("Hello Interfaces"); } }
Interface ABC {void XYZ (); }
Output:
| p4.cs (1,7): Error CS0535: ' D Emo ' does not implement Interface member ' ABC.XYZ () ' P4.cs (11,8): (Location of the symbol related to previous error) |
p6.cs
program code:
class demo:abc { public static void Main () {&N Bsp system.console.writeline ("Hello Interfaces"); }
void xyz () { system.console.writeline ("in XYZ"); }
interface ABC { void xyz ();
output:
| A.cs (1,7): Error CS0536: ' Demo ' does not implement Interface member ' ABC.XYZ () '. ' DEMO.XYZ () ' is either static, isn't public, or has the wrong return type. A.cs (16,8): (Location of the symbol related to previous error) A.cs (7,8): (Location of the symbol related to previous error) |
there's another mistake! The class demo implements the method XYZ but does not have sufficient access rights. The method that is defined on the interface ABC access rights is public. Look at the code below.
P7.cs Program code: Class Demo:abc {public static void Main () {Demo demo = new Demo (); System.Console.WriteLine ("Hello Interfaces"); DEMO.XYZ (); }
public void xyz () {System.Console.WriteLine ("in XYZ"); } }
Interface ABC {void XYZ (); }
Output:
Okay, good! The above code compilation runs successfully to produce the expected output results. As mentioned earlier, the interfaces can call different classes that implement the same interface. Therefore, different classes that implement the same interface are required. In the code above, the class demo implements the interface ABC. Let's make the other class sample also implement interface ABC.
P8.cs
Program code:
Class Demo:abc {public static void Main () {System.Console.WriteLine ("Hello Interfaces"); Demo Refdemo = new Demo (); REFDEMO.XYZ (); Sample refsample = new sample (); REFSAMPLE.XYZ (); }
public void xyz () {System.Console.WriteLine ("in Demo:: XYZ"); } }
Interface ABC {void XYZ (); }
Class Sample:abc {public void xyz () {System.Console.WriteLine ("in Sample:: XYZ"); } }
Output:
| In Demo:: XYZ in Sample:: XYZ |
The above program compiles the desired output from the successful production run. Refdemo is an example of a class demo. Refsample is an instance of class sample. All two of these classes implement the interface ABC so they all implement the method XYZ (). The XYZ () method for class demo and class sample is called from the program Portal main () method through Refdemo and Refsample instances.
Now that there are two different classes that implement the same interface, this shows how to use the same interface reference from different classes.
P9.cs
Program code:
Class Demo:abc {public static void Main () {System.Console.WriteLine ("Hello Interfaces"); ABC refabc = NULL; REFABC = new Demo (); REFABC.XYZ (); REFABC = new Sample (); REFABC.XYZ (); }
public void xyz () {System.Console.WriteLine ("in Demo:: XYZ"); } }
Interface ABC {void XYZ (); }
Class Sample:abc {public void xyz () {System.Console.WriteLine ("in Sample:: XYZ"); } }
Output:
| In Demo:: XYZ in Sample:: XYZ |
The above code compilation run program produces the expected output result. The interface reference REFABC is an interface ABC type defined in the main () method. The XYZ () that is instantiated as demo in REFABC stores the class demo class definition can be called by REFABC. Next, it is instantiated as Sample XYZ () that stores the class sample class definition in Refabc, which can be called by REFABC. Therefore, REFABC can be referenced through a common interface to access different classes of the demo and XYZ () methods in sample.
In the following code, use the Loop call class demo and sample to implement the same interface ABC using a single interface to reference the implementation of the REFABC type matching interface ABC class.
P10.cs
Program code:
class demo:abc { public static void Main () { abc[] refabc = {new Demo (), New Sample ()}; for (int i = 0; I <= 1; i++) REFABC . XYZ (), }
public void xyz () { system.console.writeline ("in Demo:: XYZ "); }
interface ABC { void xyz ();
class Sample:abc { public void xyz () { &N bsp; system.console.writeline ("in Sample:: XYZ"); }
output:
| In Demo:: XYZ in Sample:: XYZ |
The above code compilation run program produces the expected output result. REFABC is an array of type ABC interfaces. It holds references to objects of the class demo and sample. In the For loop, you can invoke the method xyz () in the class demo and sample using the number REFABC. A class can implement multiple interfaces. Look at the program below.
P11.cs
Program code:
Class DEMO:ABC, def {public static void Main () {System.Console.WriteLine ("Hello Interfaces"); ABC refabc = new Demo (); REFABC.XYZ (); }
public void xyz () {System.Console.WriteLine ("in XYZ"); }
public void Pqr () {System.Console.WriteLine ("in XYZ"); } }
Interface ABC {void XYZ (); }
Interface def {void PQR (); }
Output:
The above code compilation run program produces the expected output result. The class demo implements the interface ABC and implements the XYZ () method. The class demo also implements the Def interface and implements the PQR () method. REFABC is an instance of a class demo that is a variable of type ABC interface. The XYZ () method in the demo can be called through an instance of REFABC like REFABC is the type of interface ABC contains the prototype of the XYZ () method
P12.cs
Program code:
Class DEMO:ABC, def {public static void Main () {System.Console.WriteLine ("Hello Interfaces"); ABC refabc = new Demo (); REFABC.XYZ (); REFABC.PQR (); }
public void xyz () {System.Console.WriteLine ("in XYZ"); }
public void Pqr () {System.Console.WriteLine ("in XYZ"); } }
Interface ABC {void XYZ (); }
Interface def {void PQR (); }
Output:
| P11.cs (9,5): Error CS0117: ' abc ' does not contain a definition for ' PQR ' |
Error! An attempt was made to access the PQR () method by defining a demo instance of the variable refabc of the interface ABC type, including the prototype of function XYZ () in interface ABC but not the PQR () method prototype. You can approach the PQR () method by typing the demo instance of the interface def because the interface def contains the prototype of the method PQR ().
Examples of C # interfaces