interface with properties c# code example
Example 1: c# interface properties
public interface ISampleInterface
{
string Name
{
get;
set;
}
}
Example 2: creating interface in C#
using System;
namespace Grepper_Docs
{
public interface IWhatever
{
bool doSomething();
}
class Program : IWhatever
{
static void Main(string[] args)
{
var pro = new Program();
pro.doSomething();
}
public bool doSomething()
{
return true;
}
}
}
Example 3: interface property implementation c#
using System;
interface IName
{
string Name { get; set; }
}
class Employee : IName
{
public string Name { get; set; }
}
class Company : IName
{
private string _company { get; set; }
public string Name
{
get
{
return _company;
}
set
{
_company = value;
}
}
}
class Client
{
static void Main(string[] args)
{
IName e = new Employee();
e.Name = "Tim Bridges";
IName c = new Company();
c.Name = "Inforsoft";
Console.WriteLine("{0} from {1}.", e.Name, c.Name);
Console.ReadKey();
}
}