Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
Search :       Advanced Search �
Home

Author Rank :
Page Views :
Downloads : 0
Rating :
 Rate it
Level :
Become a Sponsor
Tags


Introduction

 

An interface looks like a class, but has no implementation. The only thing it contains are definitions of events, indexers, methods and/or properties. The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined.

 

Interface characteristics

 

  • An interface defines only the properties and method signatures and not the actual implementation. In a class declaration we implement fields, properties, methods and events. In an interface we just define what properties/methods the class should have.
  • Requires that classes which implement an interface must "honour" the property and method signatures. Basically are like contracts for classes. Classes which implement an interface must implement the methods and the properties of the interface.
  • Many classes can implement a particular interface method, in their own way
  • An interface may inherit from multiple base interfaces
  • A class may implement-inherit from multiple interfaces
  • A C# class may only inherit from one class

 

Declaration

 

[attributes] [modifiers]

            interface identifier [:base-type[,]]

            {

                 body [;]

            }

 

The attributes is optional and is used to hold additional declarative information.

 

The modifier is optional. The allowed modifiers are public and internal, unless nested inside a class, then the allowed modifiers are public, protected, private and internal. If no modifier is supplied then a default of internal is used.

 

The keyword interface must be followed by an identifier that names the interface.

 

The base-type of an interface can be zero or more interfaces. When more than one base-type is used, then this is a comma-separated list of base-types.

 

The body contains the member declarations.

 

Example

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace Interface

{

    public class Mammal

    {

        protected string Characteristis;

        public string characteristics

        {

            get

            {

                return this.Characteristis;

            }

            set

            {

                this.Characteristis = value;

            }

        }

    }

    interface IIntelligence

    {

        /// Interface method declaration

        bool intelligent_behavior();

    }

 

    class Human : Mammal, IIntelligence

    {

        public Human()

        {

            characteristics = "Human are mammals";

        }

 

        /// Interface method definition in the class that implements it

        public bool intelligent_behavior()

        {

         Console.WriteLine("{0} and have intelligence",characteristics);

         return true;

        }

    }

    class Whale : Mammal

    {

        public Whale()

        {

            characteristics = "Whale are mammals";

            Console.WriteLine("{0}", characteristics);

        }

    }

    class InterfaceApp

    {

        public static void Main(string[] args)

        {

            Whale whale = new Whale();

            Human human = new Human();

            /// The human object is casted to the interface type

            IIntelligence humanIQ = (IIntelligence)human;

            humanIQ.intelligent_behavior();

 

            Console.Read();

        }

    }

}

 

First letter I

 

Here we mention that in the C# programming language, interface types are usually named with a first letter of I. There is no language-based constraint on this. Instead, this is a convention and as usual with conventions.

 

Look Inside

 

In C#, an interface is a first-class concept that declares a reference type that includes method declarations only. The term "first-class concept" means that the feature in question is a built-in, integral part of the language.

 

When to Use Interfaces

 

Interfaces allow us to create nice layouts for what a class is going to implement. Because of the guarantee the interface gives us, when many components use the same interface it allows us to easily interchange one component for another which is using the same interface. Dynamic programs begin to form easily from this.

 

Summary

 

You have seen the implementation of Interfaces in C#. You can implement an interface and use it in a class. Interfaces may also be inherited by other interface. Any class or struct that inherits an interface must also implement all members in the entire interface inheritance chain.

 

 [Top] Rate this article
 
 About the author
 
Author
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
 Comments

 � 2025  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.