IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#OOP   Print This     
  From the October 2015 Issue of Prestwood eMag
 
C# OOP:
C# Interfaces (interface)
 
Posted 16 years ago on 10/24/2008 and updated 2/21/2009
C# Code Snippet:
 A flashcard from our C# Flashcards Library
 A code snippet from our C# Code Snippets Page

KB101435

General Info: Interface

An element of coding where you define a common set of properties and methods for use with the design of two or more classes.

Both interfaces and abstract classes are types of abstraction. With interfaces, like abstract classes, you cannot provide any implementation. However, unlike abstract classes, interfaces are not based on inheritance. You can apply an Interface to any class in your class tree. In a real sense, interfaces are a technique for designing horizontally in a class hierarchy (as opposed to inheritance where you design vertically). Using interfaces in your class design allows your system to evolve without breaking existing code.

C# Interfaces

Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, but a class or struct can inherit more than one interface and it inherits only the method names and signatures, because the interface itself contains no implementations.

class MyClass: IMyInterface
{  
  public object Clone()
{
return null;
}

// IMyInterface implemented here...
}
Syntax Example:
interface IMyInterface
{
  bool IsValid();
}

Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, but a class or struct can inherit more than one interface and it inherits only the method names and signatures, because the interface itself contains no implementations.

class MyClass: IMyInterface
{  
  public object Clone()
{
return null;
}

// IMyInterface implemented here...
}

Uses of Interfaces

  • Interface-Based Polymorphism (Substitutability) - Substitutability allows a descendant class to be used anywhere an associated parent class is used. In object oriented programming a variable could refer to one object at one time and then another object another time. This allows the designer of software to create both a dog.run and a cat.run methods and then decide at runtime whether the variable will be a dog or a cat. Interfaces are a common coding element used to implement this type of polymorphism.
     
  • Interfaces allow for horizontal class design. In a class tree, inheritance is used to design classes vertically with "is-a" relationships. You add functionality from top to bottom adding methods and properties to decsendant classes. Interfaces allow you to design horizontally across your class tree using a "behaves-as" or "looks-like" relationship insisting certain classes implement a common interface.

Visual C# 2008 Working WinForms Example

The following example demonstrates implementing a very simple interface. The interface is named IHuman which includes one property and one method. Our resulting class is named CyborgHuman and, for clarity, our CyborgHuman class also inherits from a class called Cyborg.

Create a form and place a button on it and alter the code as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace CR_Interfaces
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
  
    private void button1_Click(object sender, EventArgs e)
    {
      CyborgHuman MyRobot = new CyborgHuman();
      MyRobot.HumanName = "Nicole";
      MyRobot.Speak("Hi, my name is " + MyRobot.HumanName + ".");
    }
  }
  
  public interface IHuman
  {
    //Interface property.
    string HumanName {get; set;}
  
    //Interface method.
    void Speak(string pMessage);
  }
  
  //Sample class for demonstration.
  public class Cyborg
  {
  }
  
  public class CyborgHuman: Cyborg, IHuman
  {
    public string HumanName {get; set;}
    public void Speak(string pMessage)
    {
      MessageBox.Show(pMessage);
    }
  }
}

Default Interface Visibility: Internal

If you do not specify an interfaces visibility, the default is Internal (accessible from types in the same assembly) but an interface's members are always public -- which makes sense but is noteworthy.

interface ITalk {..} //Default visibility is Internal.

More Info

Definition:  Interface

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1153A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101435 Counter
18228
Since 10/24/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]