Search code examples
c#.netinterfaceextend

add property to interface


I have an interface (from another Framework that I cannot directly modify) that looks like the following:

public interface IUserDemo
{
string UserName { get; }
}

I would like to 'extend' this interface so it looks like the following:

public interface IUserDemo
{
string UserName { get; }
string Password { get; }
}

The solution will hopefully allow me to do the following:

  UserDemo demouser = new UserDemo();
  return new UserDemo
   {            
      UserName = userName,
      Password = password
   };

Where UserDemo simply looks like:

public class UserDemo : IUserDemo
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

If anyone can nudge me in the right direction that would be great!


Solution

  • You can extend IUserDemo like the following

    public interface IMyUserDemo : IUserDemo
    {
        string Password { get; }
    }
    

    Implement your extended interface IMyUserDemo

    public class UserDemo : IMyUserDemo
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    }