Skip to main content

Interface in C#

Interface


An interface is a signature.It is not a class

 Properties
It contains definitions or signature of a method and the implementation of these methods are provided by the class inheriting the interface

A class can implement more than one interface , so interface can be used to implement multiple inheritance in c#

No object of interface can be created

It contains only properties, indexers, methods, delegates and events signature.
It cannot contains constants members, constructors, instance variables, destructors, static members or nested interfaces.
Members of an interface cannot have any access modifiers even public.
Implicitly, every member of an interface is public and abstract. Also, you are not allowed to specify the members of an interface public and abstract or virtual

It cannot be instantiated but it can be referenced by the class object which implements it. Also, Interface reference works just like object reference and behave like as object.
IStore IObjStore = new Document();
ICompress IObjCompress = new Document();

An interface can be inherited from one or more interfaces.
An interface can extend another interface.
A class or struct can implements more than one interfaces.
A class that implements an interface can mark any method of the interface as virtual and this method can be overridden by derived classes.

Why Interface?


Interface based design concept provides loose coupling
Code reuse becomes easier
easier maintaniability
componenet based programming


//Interface called IAddCallView created which inheriting from 2 more interface
public interface IAddCallView:Iview,IAddView
{
   // interface members
   void showTransaction();
   double getAmount();
  //Properties created
   string FirstName {get; set;}
   string LastName {get; set;}
}

//Our class implementing interface

Public Class Student:IAddCallView
{
 Public void showTransaction()
 {
 //Content goes here
 }
 //Interface defined
 Public void getAmount()
 {
 //Content goes here
 }
 //Properties defined
 Private string firstname;
 Public string FirstName {
 get{ return firstname;}
 set{firstname=Value; }
 }
}


How to make an interface private?


An interface could be private within another class

public class MyClass
{
    private interface IFoo
    {
        int MyProp { get; }
    }

    private class Foo : IFoo
    {
        public int MyProp { get; set; }
    }

    public static void Main(string[] args)
    {
        IFoo foo = new Foo();
        return foo.MyProp;
    }
}

Types of Interface
 1,Implicit Interface
 2,Explicit Interface

Implicit Interface
The methods are publicly implemented

  public class Person : IDal
   {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public void Add()
        {
            throw new NotImplementedException();
        }

        public void Update()
        {
            throw new NotImplementedException();
        }
    }


Explicit Interface

 In explicit interface the methods are privately implemented

  public class Person : IDal
   {
        public string FirstName { get; set; }
        public string LastName { get; set; }


        void IDal.Add()
        {
            throw new NotImplementedException();
        }

        void IDal.Update()
        {
            throw new NotImplementedException();
        }
   }

Explicit interface is a bad approach

Instead of this we can use two separate interfaces.

 It can be used when your concrete class abstraction does not include interface abstraction.

 public interface IDal
  {
        void Add();
        void Update();
  }

  public class Person : IDal
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        void IDal.Add()
        {
            // Add code goes here
        }

        void IDal.Update()
        {
            // Update code goes here.
        }
    }
//Only Properties ie public properties are available to Concrete class

Person objP=new Person();
objP.FirstName
objP.LastName
//For interface class interface methods are available.

IDal objID=new Person();
objID.Add();
objID.Update();


Comments

Popular posts from this blog

Gemini software Dot net interview question for experienced

Gemini Interview questions 4-8 year experienced Dot net professional Gemini Interview questions 4-8 year experienced Dot net professional 1,Asp .net mvc request life cycle. 2,How routing works 3,Where codes for routing are written 4,What is attribute based routing in aap.net Mvc 5,Is multiple routes possible on a single action method. 6,What is action filters. 7,what is authentication and authorization filters 8,What are the types of authentication 8,What is the use of data annotation 9,Can we fire data annotation in client side. 10,What is model binding 11,what are Html helpers

15 Essential jQuery Interview Questions

15 Essential jQuery Interview Questions 15 Essential jQuery Interview Questions 1,Explain what the following code will do: $( "div#first, div.first, ol#items > [name$='first']" ) Ans:This code performs a query to retrieve any element with the id first, plus all elements with the class first, plus all elements which are children of the element and whose name attribute ends with the string "first". This is an example of using multiple selectors at once. The function will return a jQuery object containing the results of the query. 2,What is wrong with this code, and how can it be fixed to work properly even with buttons that are added later dynamically? // define the click handler for all buttons $( "button" ).bind( "click", function() {     alert( "Button Clicked!" ) }); /* ... some time later ... */ // dynamically add another button to the page $( "html" ).append( " Click...

ASP .Net MVC

ASp .Net MVC 1. Explain MVC (Model-View-Controller) in general? MVC (Model-View-Controller) is an architectural software pattern that basically decouples various components of a web application. By using MVC pattern, we can develop applications that are more flexible to changes without affecting the other components of our application. §    “Model”, is basically domain data. §    “View”, is user interface to render domain data. §    “Controller”, translates user actions into appropriate operations performed on model. 2. What is ASP.NET MVC? ASP.NET MVC is a web development framework from Microsoft that is based on MVC (Model-View-Controller) architectural design pattern. Microsoft has streamlined the development of MVC based applications using ASP.NET MVC framework. 3. Difference between ASP.NET MVC and ASP.NET WebForms? ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front con...