Define an interface and implement the interface with a class - CSharp Custom Type

CSharp examples for Custom Type:interface

Description

Define an interface and implement the interface with a class

Demo Code

using System;//from  w  w  w .  jav a  2s. c o m
using System.Collections.Generic;
using System.Text;
public interface Runnable
{
   void Run(int distance);
}
public class Person : Runnable
{
   private readonly string _firstName;
   private readonly string _lastName;
   public string FirstName
   {
      get
      {
         return _firstName;
      }
   }
   public string LastName
   {
      get
      {
         return _lastName;
      }
   }
   public int Age { get; set; }
   public Person(string firstName, string lastName, int age)
   {
      _firstName = firstName;
      _lastName = lastName;
      Age = age;
   }
   public virtual void SayHello()
   {
      Console.WriteLine("Hello I'm a Person");
   }
   public override string ToString()
   {
      return FirstName + " " + LastName + " - age: " + Age;
   }
   public void Run(int distance)
   {
      Console.WriteLine("I run for {0} kms", distance);
   }
}
class Program
{
   static void Main(string[] args)
   {
      Person p = new Person("A", "S", 32);
      p.Run(5);
   }
}

Result


Related Tutorials