189

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;
      }
}
1
  • 1
    why do you want to implement anything in the interface? There you just specify the interface... Commented Oct 20, 2009 at 9:30

6 Answers 6

401

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How do you say that Version is public in the class? Ahh, I see my problem: if you implement the Interface explicitly (e.g. IResourcePolicy.Version, you can't define if it's public.
You're right, it should be public. I fixed it. I didn't care much about this, because it is not relevant to the things I tried to explain.
Of course it's relevant! If you want to deliver a good answer you need to make it simple for people. So they don't stumble on the next problem.
28

You mean like this?

class MyResourcePolicy : IResourcePolicy {
    private string version;

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

1 Comment

this i knows ,,but i already assigned values such that irp.WrmVersion = "10.4"; see my question,,i dont need to lose that value ,,i have to pass it
21

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

5 Comments

Thats what how can i implement this property somewhere else
create a class @petr that implements that interface,than create instance to that class and assign to variable which have interface type
I would vote for the first sentence. But abstract base classes should always be avoided if an interface is enough. So before you know what problem should actually be solve, you shouldn't suggest to create a base class.
@Stefan, by second sentence I meant that if he really needs a default value to be introduced, then he should use abstract class.
This is no longer true with c#8/.net core 3.0. Interfaces still cannot have global variables though.
7

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

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.