Skip to content

Instantly share code, notes, and snippets.

@dnasca
Created March 17, 2015 06:30
Show Gist options
  • Select an option

  • Save dnasca/de974914a5dce8f3d045 to your computer and use it in GitHub Desktop.

Select an option

Save dnasca/de974914a5dce8f3d045 to your computer and use it in GitHub Desktop.
Interface Basics
using System;
/* //Interface Basics//
*
* Interfaces can contain properties, methods, delegates or events but ONLY the declarations. NOT implementations.
*
* Interface members are public by default and DO NOT allow explicit access modifiers
*
* Interfaces CANNOT contain fields
*
* If a class or a struct inherits from an interface, it MUST provide implementation for all interface members
*
* A class or struct can inherit from more than one interface at the same time
*
* Interfaces can inherit from other interfaces. If using multiple interfaces, you MUST provide implementation for all interface members of the interface chain
*
* You CANNOT create an instance of an interface, but an interface reference variable can point to a derived class object.
*
*/
interface IPrint
{
void Print();
}
interface IAlsoPrint
{
void AlsoPrint();
}
public class PrintImpl : IPrint, IAlsoPrint //using multiple interfaces, all interface members must be implemented
{
public void Print()
{
Console.WriteLine("IPrint interface Print method called");
}
public void AlsoPrint()
{
Console.WriteLine("IAlsoPrint interface AlsoPrint method called");
}
}
public class Program
{
public static void Main()
{
PrintImpl P = new PrintImpl();
P.Print();
P.AlsoPrint();
//example of a class inheriting from an interface
IPrint PP = new PrintImpl();
PP.Print();
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment