C# Interfaces

In this chapter you will learn:

  1. What are C# Interfaces
  2. How to define an interface
  3. Note for Interfaces

Description

An interface provides a specification rather than an implementation for its members.

A class can implement multiple interfaces. In contrast, a class can inherit from only a single class.

Interface members are all implicitly abstract.

Structs can implement interfaces.

Syntax

Interfaces are declared by using the interface keyword. Here is a simplified form of an interface declaration:


interface name {// ww  w  .  ja v a2s  .  c om
   ret-type method-name1(param-list);
   ret-type method-name2(param-list);
   // ...
   ret-type method-nameN(param-list);
}

Note

Interface members are always implicitly public and cannot declare an access modifier.

Implementing an interface means providing a public implementation for all its members.

You can implicitly cast an object to any interface that it implements.

Next chapter...

What you will learn in the next chapter:

  1. How to implement an interface
  2. Code to show how to create and implement interface
  3. Multiple Implementation: implement two interfaces
  4. extend class and implement interface at the same time
Home »
  C# Tutorial »
    C# Types »
      C# Interface
C# Interfaces
C# interface implementation
C# Explicit interface
C# Interface inheritance
C# Indexer in an interface
C# Interface Properties
C# struct and interface
C# virtual interface