Add 'set' to properties of interface in C#

前端 未结 3 486
广开言路
广开言路 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:58

    You could use an abstract class:

    interface IUser
    {
        string UserName
        {
            get;
        }
    }
    
    abstract class MutableUser : IUser
    {
        public virtual string UserName
        {
            get;
            set;
        }
    }
    

    Another possibility is to have this:

    interface IUser
    {
        string UserName
        {
            get;
        }
    }
    
    interface IMutableUser
    {
        string UserName
        {
            get;
            set;
        }
    }
    
    class User : IUser, IMutableUser
    {
        public string UserName { get; set; }
    }
    

提交回复
热议问题