In today's section, we'll talk about Interfaces. Interfaces are constraints implementing class must follow. In one way, it is similar to abstract class but doesn't act as base class. C# doesn't have multiple inheritance. You can inherit from only one class but you can implement as many interfaces as you like. Also, one point to understand that access modifiers like private, public, protected, etc are not legal in case of interfaces. Hence, without wasting time, let's jump at the demo.
The following is the simple interface in its plain form.
- namespace InterfaceDemo
- {
- interface ITransaction
- {
- void doTransaction();
- double getAmount { get; }
- }
- }
- namespace InterfaceDemo
- {
- class Transaction :ITransaction
- {
- public void doTransaction()
- {
- throw new System.NotImplementedException();
- }
- public double getAmount { get; private set; }
- }
- }
- using System;
- namespace InterfaceDemo
- {
- class Transaction : ITransaction
- {
- //Not part of interface
- public void startTransaction()
- {
- Console.WriteLine("Started doing Transaction");
- }
- public void doTransaction()
- {
- Console.WriteLine("Doing Cash Transaction");
- }
- public double getAmount
- {
- get { return 10000; }
- }
- }
- }

The following is the main class in its finished form.
- using System;
- namespace InterfaceDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Transaction transaction = new Transaction();
- transaction.startTransaction();
- transaction.doTransaction();
- Console.WriteLine(transaction.getAmount);
- Console.ReadLine();
- }
- }
- }

However, It is also perfectly legal to have a variable of type Interface and instantiate a class like shown below. But, while doing so, here I won't be having method access which is declared and implemented explicitly in class.

- using System;
- namespace InterfaceDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- ITransaction transaction = new Transaction();
- transaction.doTransaction();
- Console.WriteLine(transaction.getAmount);
- //Transaction transaction = new Transaction();
- //transaction.startTransaction();
- //transaction.doTransaction();
- //Console.WriteLine(transaction.getAmount);
- Console.ReadLine();
- }
- }
- }

Also, we can have main method like shown below.
- using System;
- namespace InterfaceDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- var transaction = new Transaction();
- transaction.startTransaction();
- transaction.doTransaction();
- Console.WriteLine(transaction.getAmount);
- //Assigned the object to Interface variable
- ITransaction iTransaction = transaction;
- //Here also only interface stuffs will be available
- //However, if at all we would like to transaction explicit method available, then we need to cast like shown below
- Transaction transactionObj = iTransaction as Transaction;
- //Here, as keyword will examine the iTransaction and if it is of type Transaction, then it will return Transaction
- //else will return null. Hence, null check is recommended here
- if(transactionObj!=null)
- transactionObj.startTransaction();
- Console.ReadLine();
- }
- }
- }

Download link.
With this, I would like to wrap this session here. Thanks for joining me.

Ankit SaxenaPosted Jan 11, 2016, 8:07 AM
Good post
Luca Spano'Posted Dec 16, 2015, 5:28 PM
Nice post
Vignesh ManiPosted Dec 14, 2015, 3:25 AM
Nice
Humayun Kabir MamunPosted Dec 14, 2015, 1:17 AM
Nice...
Raja TPosted Dec 13, 2015, 11:12 PM
Nice,thank for sharing
Ankur MistryPosted Dec 13, 2015, 1:48 PM
Nice share