News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Jan 18 2015 5:28AM     Mohammad Shaik
  0 Comments    1600 Views  
Here Mohammad Shaik provided a short tutorial with example, about Interface in C#.
What is an Interface in C#?
  1. An Interface is a way to achieve runtime polymorphism in C#.
  2. An Interface is a collection of properties and methods declarations.
  3. An Interface is a type declaration like a class or structure in C#; an Interface itself does not implement members.
  4. Interfaces are contract templates in which the contract members are declared.
Characteristics of an Interface
Interfaces cannot be initialized directly as they do not have a definition for members, they should be implemented by either a class or structure.

Interfaces can contain any of the following type members or a combination of these four member types.
  1. Property
  2. Method
  3. Event
  4. Indexer
An Interface can be declared as either public or internal inside a namespace and can’t have any other access modifiers. An Interface can have any access modifier when declared inside a class or structure.
Members of an Interface are public by default and can’t have other access modifiers.
A class implementing an Interface must implement all the members declared in the Interface and they must be marked as public in the Implicit implementation.
In the case of Explicit implementation, the Interface members can't have any access modifiers and they are private by default.
If the implementing class is signed as abstract then the members of the Interface can also be signed as abstract in the class.
Declaring Interfaces
Interfaces are declared using the interface keyword. It is similar to class declaration. Interface statements are public by default. Following is an example of an interface declaration:

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

Example
The following example demonstrates implementation of the above interface:

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

namespace InterfaceApplication
{

   public interface ITransactions
   {
      // interface members
      void showTransaction();
      double getAmount();
   }
   public class Transaction : ITransactions
   {
      private string tCode;
      private string date;
      private double amount;
      public Transaction()
      {
         tCode = " ";
         date = " ";
         amount = 0.0;
      }
      public Transaction(string code, string date, double amount)
      {
         tCode = code;
         date = date;
         amount = amount;
      }
      public double getAmount()
      {
         return amount;
      }
      public void showTransaction()
      {
         Console.WriteLine("Transaction Code: {0}", tCode);
         Console.WriteLine("Transactin Date: {0}", date);
         Console.WriteLine("Transaction Amount: {0}", getAmount());

      }

   }
   class Tester
   {
      static void Main(string[] args)
      {
         Transaction objT1 = new Transaction("00124""7/11/2014", 77900.00);
         Transaction objT2 = new Transaction("00125""8/11/2014", 424900.00);
         objT1 .showTransaction();
         objT2 .showTransaction();
         Console.ReadKey();
      }
   }
}

Result
When the above code is compiled and executed, it produces the following result:

Transaction Code: 001
Transaction Date: 8/10/2012
Transaction Amount: 78900
Transaction Code: 002
Transaction Date: 9/10/2012
Transaction Amount: 451900

I hope this page will helps to learn Interfaces in C#. Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: