Add 'set' to properties of interface in C#

前端 未结 3 517
广开言路
广开言路 2021-02-07 04:16

I am looking to \'extending\' an interface by providing set accessors to properties in that interface. The interface looks something like this:

interface IUser
         


        
3条回答
  •  故里飘歌
    2021-02-07 04:41

    I don't see any reason why what you have posted shouldn't work? Just did a quick test and it compiles alright, but gives a warning about hiding. This can be fixed by adding the new keyword, like this:

    public interface IMutableUser : IUser
    {
        new string Username { get; set; }
    }
    

    An alternative would be to add explicit set methods; eg:

    public interface IMutableUser : IUser
    {
        void SetUsername(string value);
    }
    

    Of course, I'd prefer to use setters, but if it's not possible, I guess you do what you have to.

提交回复
热议问题