What is an interface in C#?

CsharpProgrammingServer Side Programming

Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.

Let us see how to declare interface in C# with interface members −

public interface InterfaceName {
   // interface members
}

The following is an example showing how to use Interface in C# −

Example

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;

namespace Demo {

   public interface ITransactions {
      // interface members
      void showTransaction();
   }

   public class Transaction : ITransactions {
      private string tCode;
      private string date;

      public Transaction() {
         tCode = " ";
         date = " ";
      }
   
      public Transaction(string c, string d) {
         tCode = c;
         date = d;
      }

      public void showTransaction() {
         Console.WriteLine("Transaction ID: {0}", tCode);
         Console.WriteLine("Date: {0}", date);
      }
   }

   class Tester {

      static void Main(string[] args) {
         Transaction t1 = new Transaction("8877", "6/25/2018");
         Transaction t2 = new Transaction("5656", "7/25/2018");

         t1.showTransaction();
         t2.showTransaction();
         Console.ReadKey();
      }
   }
}
raja
Published on 13-Aug-2018 16:01:36
Advertisements