0

Which one to create - an interface with just properties or a class with those properties? What are the deciding factors? Any problem if I choose one over another?

There wont be any methods; there will be properties only.

An interface:

public interface IUnit
{
    IEnumerable<Dept> Departments { get; }

    Owner Owner { get; }
}

Or a class:

public class Unit
{
    public IEnumerable<Dept> Departments { get; }

    public Owner Owner { get; }
}
Learner
  • 4,661
  • 9
  • 56
  • 102
  • 3
    This decision is up to you and depends on your requirements, class cam be instantiated, interface must be implemented – Pavel Anikhouski Feb 20 '20 at 08:39
  • Use interface only if there are multiple classes which are going to have same functionalities but result might be different. For example: create printer interface with basic set of functionalities like print, cancel and delete that you may want to give access to one module which can further be implemented by different printer classes available on your network. Additionally you can create another printer interface with advance set of functionalities like print, cancel, delete, page setup etc which can be access by advance modules using same printer classes. – maddy23285 Feb 20 '20 at 08:46
  • Extending my previous comment:- Your Printer classes will have all functionalities from basic to advance. Interface helps to restrict the user to access only those functionalities which is required according to their role in application. Moreover, you can create multiple printer classes for different type of printers installed on your network and give limited access to users with the help of interfaces. – maddy23285 Feb 20 '20 at 08:50
  • You only need an interface when you have a piece of code that needs to work with multiple concrete types in the same way. If you don't have that, or aren't planning on it (read, *not* creating a general purpose library for the world to use), you don't need interfaces. Otherwise, it's just more code, which is bad. – jmrah Feb 20 '20 at 15:22

0 Answers0