C# tutorial – What is C# interface

5
  Uncategorized

C# tutorial : What is C# interface? – C# interface is programming construct which describes a group of related functionalities that can belong to any C# class or struct.

C# interface can be declared as follows:

interface IExampleInterface

{

string Property { get; set; }

void Function();

Event SampleEvent;

string this[int position]  { get;  set; }

}

C# interface can consist of:

  • Properties
  • Indexers
  • Events
  • Methods
  • Combination of above four

C# interface can not have:

  • Constants
  • Constructor
  • Static members
  • Operators
  • Function definitions

C# interface quick facts

  • All interfaces are by default public and abstract
  • All interface members also are public and can not have any access modifier
  • All non abstract classes or structure which implement interface must have definitions for al interface members and must match the signature.
  • Interface instance can not be created
  • Interface can inherit other interfaces
  • Classes and structs can inherit multiple interfaces
  • If method with same signature present in two or more interface then class can define InterfaceName.Method to differentiate
  • Classes withing different hierarchy can implement same interface
  • Interfaces can not be partially implemented. All interface members must be implemented.
  • If you are using PRISM then interfaces provides easier dependency injection mechanism
  • Interface provides guarantee of basic functionality and provides a contract
  • Interface also make your application scalable
  • Interface helps in providing standards to implementers
  • Interface provides run-time polymorphism
  • Relationship of class with interface is implements (Can do this) and not similar to abstract base class (IS-A)

How to implement interface in C# ?

public class ExampleClass : IExampleInterface

{

All definitions of interface members is must.

TIP: In Visual studio you can hover mouse over interface name in class file and you can implement all members of interface in one click.

}

Disadvantages of interface : When not to use interface?

  • When you want IS-A relationship
  • When class wants default functionality of some members
  • if members are going to change rapidly

5 thoughts on “C# tutorial – What is C# interface

  1. I’m so happy to read this. This is the type of manual that needs to be given and not the accidental misinformation that’s at the other blogs. Appreciate your sharing this greatest doc.

  2. Pingback: penis traction

  3. I don’t even understand how I finished up right here, but I believed this put up used to be good. I don’t know who you are however certainly you are going to a famous blogger in the event you aren’t already 😉 Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *