abcprep.com - your onestop GMAT, GRE, PMP hub
.NET FAQs Unleashed!
 
    



    


What is an interface in C#?

An Interface is a named collection of semantically similar abstract members or functions. A class or a structure may follow a behaviour that is prescribed by an interface. The keyword interface is used to create an interface. Note that while defining members inside an interface, there is no need to specify the access specifier, as they are public by default.

See syntax below:

public interface IBooks

{

  long int ISDN_No;

}

As a convention, interfaces in .NET are prefixed with an "I" in uppercase, as a good programming practice.

Methods inside an interface do not have a body. They only signify that such a method should exist in the implementing class or structure.

Classes that display a common set of features, such as having similar types of member variables or member functions, are designed in such a way that they implement a common interface.

How to implement an interface in C#?

An interface may be implemented using the keyword "implements" in VB.NET, and using a colon in C#. See code C# example below:

//In C#

interface IBooks
{
    long int Cost { get; } //Interface property  
}

//ComputerBooks is a class that implements an Interface IBooks

public class ComputerBooks : IBooks

{

  // Implementation of the property

  public long int Cost 
  {
        get { return 100; } //Read only value
    }

}

What is the Difference between an Interface and an Abstract Class

Protected Internal  Array  Sort Array  Throw  Multiple Catch  Polymorphism  Inheritance  Session Viewstate  Autogenerate  ReadOnly  Sealed Class  Sealed Method  Multiple Interfaces  Overloading  Overloaded Constructors  Generics  Main  Case Sensitive  Console  Specifier  Return Type  SetCommandLineArgs  System Environment  New  Default Values of Types Compiler Error Constant Variable Const Const - ReadOnly Parameter Modifier Out Ref Interface Interface Reference, is, as System.Collection

More Interview Questions...
Skip Navigation Links
  Design Patterns  
  Ajax
  Security
  C#
  ASP.NET
 ASP.NET 2.0
 Articles
SQL
Skip Navigation Links
  OOPs 
  Framework
  ADO.NET
  100 Short Q-As
  Difference Between Q-As
  .NET Acronyms
  Discussions








Know Your Breath, Know Your Life!!!