0

I have the following interface..It has a error which i had no idea why it happened. Basically for the interface partstring DoorDescription { get; private set; } has to remove the private set to make it work

namespace test6
    {
        interface IHasExteriorDoor
        {
            string DoorDescription { get; private set; }
            string DoorLocation { get; set; }
        }
        class Room : IHasExteriorDoor
        {
            public Room(string disc, string loc)
            {
                DoorDescription = disc;
                DoorLocation = loc;
            }
            public string DoorDescription { get; private set; }
            public string DoorLocation { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Room a = new Room("A","B");
                a.DoorLocation = "alien";
                //a.DoorDescription = "mars";
            }
        }
    }
baozi
  • 679
  • 10
  • 30
  • @MitchWheat my question is both the interface part and class part use `get;private set;` why it didn't work.. – baozi Jul 06 '14 at 01:53

2 Answers2

3

Please see here

An interface can't contain constants, fields, operators, instance constructors, destructors, or types. Interface members are automatically public, and they can't include any access modifiers. Members also can't be static.

Basically an interface is a public contract.

You can set your property as read-only and then have a private set in the class which implements it.

Community
  • 1
  • 1
seangwright
  • 17,245
  • 6
  • 42
  • 54
2

An interface can't have any private methods.

Just remove the setter from the interface:

    interface IHasExteriorDoor
    {
        string DoorDescription { get; }
        string DoorLocation { get; set; }
    }

The class implementing it can still have a setter for the property, and as the setter is not defined in the interface, it can be private.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005