[c#] How to implement a property in an interface

I have interface IResourcePolicy containing the property Version. I have to implement this property which contain value, the code written in other pages:

IResourcePolicy irp(instantiated interface)
irp.WrmVersion = "10.4";

How can I implement property version?

public interface IResourcePolicy
{
   string Version
      {
          get;
          set;
      }
}

This question is related to c# .net

The answer is


The simple example of using a property in an interface:

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();
    }
}
/*output:
 Tim Bridges from Inforsoft.
 */

You mean like this?

class MyResourcePolicy : IResourcePolicy {
    private string version;

    public string Version {
        get {
            return this.version;
        }
        set {
            this.version = value;
        }
    }
}

You should use abstract class to initialize a property. You can't inititalize in Inteface .


Interfaces can not contain any implementation (including default values). You need to switch to abstract class.


  • but i already assigned values such that irp.WrmVersion = "10.4";

J.Random Coder's answer and initialize version field.


private string version = "10.4';