C# Interface Examples

C# Programming Examples - interfaces

In C#, an interface is a blueprint of the class. An interface is similar to an abstract class that contains only abstract methods and properties. It is not likely to have a method body or be instantiated. The implementation of the members of an interface is specified by a class that implicitly or explicitly implements the interface. To implement an interface member, the implementing class must be public, non-static, and have the same name and signature as the interface member.

C# Interface Syntax:

interface  <interface_name>
{
    // declare Events, indexers, methods & properties
}

Advantage of C# Interface:

  • Interfaces helps to achieve abstraction in C#.
  • Interfaces are used to achieve multiple inheritance.
  • Interfaces are used to achieve loose coupling.
  • Interface defines the rules that a class must work with.
  • Interfaces are used to achieve component-based programming.

iList Interface:

advertisement
advertisement

iList Interface is a collection of objects that are used to access each element individually using the index.

IDumpable Interface:

IDumpable Interface represents a collection of dumpable objects.

IDumpable Interface:

The IDictionary Interface represents a generic collection of key/value pairs. For each pair, a unique key is required.

IEnumerable Interface:

IEnumerable Interface provides an enumerator, which supports a simple iteration over a non-generic collection.

advertisement

The following section contains C# programs on interfaces, interface types, interface properties, iList Interface, iDictionary Interface, IComparable Interface, and IDumpable Interface. Every example program includes the problem description, problem solution, C# code, program explanation, and run-time test cases. All C# Interface Examples have been compiled and tested on Visual Studio.

Here is the listing of C# Interface programs with examples:

C# Programs on Interface Types

Program Description
Interface Properties in C# C# Program to Demonstrate Properties of the Interface
Interface Transactions in C# C# Program to Demonstrate Transactions using Interface
iList Interface in C# C# Program to Demonstrate the iList Interface
IDumpable Interface in C# C# Program to Demonstrate the IDumpable Interface
iDictionary Interface in C# C# Program to Demonstrate the iDictionary Interface
IComparable Interface in C# C# Program to Implement IComparable Interface
Handling Interface Event in C# C# Program to Declare an Event Handler in an Interface
IEnumerable Interface using LINQ in C# C# Program to Implement IEnumerable Interface using LINQ

Sample C# Interface Program:

iList Interface in C#

/*
 *  C# Program to Demonstrate iList Interface
 */
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        int[] a = new int[3];
        a[0] = 1;
        a[1] = 2;
        a[2] = 3;
        Display(a);
 
        List<int> list = new List<int>();
        list.Add(5);
        list.Add(7);
        list.Add(9);
        Display(list);
        Console.ReadLine();
    }
 
    static void Display(IList<int> list)
    {
        Console.WriteLine("Count: {0}", list.Count);
        foreach (int num in list)
        {
            Console.WriteLine(num);
        }
    }
}

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.