Free Web Development Online Tutorials, Learn to Code
C# Interface Example, Implement Interface in c#, C# Development Guide

C# Interface Example

In this c# interface tutorial we learn about C# Interface and Abstract Class, difference between interface and abstract class, so you know when to use interface and abstract class in C#.

What is Interface in C# ?

An interface in C# is the design of any component that only contains the declaration of the methods, properties, and events, interface never contains the implementation part.

C# interface implementation part is done in the class that implements, class:Interface. Interface keep the clean design information, thus makes it easy to maintain a standard information for all.

How to write an Interface in C#?

As you have read that Interface contain only declaration but not implementation, below is an example of interface that contain design declaration about training

C# Interface Body

interface ITraining
    {
        string Title { get; set; }
        string Subjects { get; set; }
        string Trainer { get; set; }
        string Location { get; set; }
        string TrainingDate { get; set; }
    }

If you see notice in above ITraining interface, there is only declaration about training, but no implementation

C# interface implementation example

Now let’s see how the implementation is done.
Think about some real-time scenario, we can have multiple training going on in any organization, for example .Net Training, here is the implementation.

class DotNetTraining : ITraining
{             
    string _trainer = "WebTrainingRoom";            
    string _title = ".NET Training Room";             
    string _subjects = "Interface, Abstract Class, OOP";             
    string _location = "Online";             
    DateTime _tDate = DateTime.Now;             
                
public string Title            
{             
    get { return _title; }             
    set { _title = value; }             
}
             
                
public string Subjects             
{           
    get { return _subjects; }             
    set { _subjects = value; }             
}
             
                
public string Trainer             
{             
    get { return _trainer; }             
    set { _trainer = value; }             
}
             
                
public DateTime TrainingDate             
{             
    get { return _tDate; }             
    set { _tDate = value; }             
}
             
}

Now similarly we can implement SQL Training, Jquery Training so on, so everywhere training design will remain same.

c# interface for properties

Look at the picture below, how interface properties have been implemented in different classes.

Hope now have good idea about interface and abstract class in C#, You should also check the difference between Abstract class and Interface.

Comment
Name
Email
Website
Subscribe
 
free C# tutorial online


C# Interface Example
Other Online Tutorial

C# .net Interview Questions Answers

.Net C# Examples | Join .Net C# Course