How to implement a property in an interface

C#.Net

C# Problem Overview


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;
      }
}

C# Solutions


Solution 1 - C#

In the interface, you specify the property:

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

In the implementing class, you need to implement it:

public class ResourcePolicy : IResourcePolicy
{
   public string Version { get; set; }
}

This looks similar, but it is something completely different. In the interface, there is no code. You just specify that there is a property with a getter and a setter, whatever they will do.

In the class, you actually implement them. The shortest way to do this is using this { get; set; } syntax. The compiler will create a field and generate the getter and setter implementation for it.

Solution 2 - C#

You mean like this?

class MyResourcePolicy : IResourcePolicy {
    private string version;

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

Solution 3 - C#

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

Solution 4 - C#

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.
 */

Solution 5 - C#

  • 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';

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpeterView Question on Stackoverflow
Solution 1 - C#Stefan SteineggerView Answer on Stackoverflow
Solution 2 - C#J. Random CoderView Answer on Stackoverflow
Solution 3 - C#Vitaliy LiptchinskyView Answer on Stackoverflow
Solution 4 - C#Bernard CzaińskiView Answer on Stackoverflow
Solution 5 - C#kazukView Answer on Stackoverflow
Solution 6 - C#Abdus Salam AzadView Answer on Stackoverflow