Unable to create two properties with the same name in C #


I have created two properties with same name but the data type is different. I am getting this error:

The type 'Fields' already contains a definition for 'Subject'

Is it possible to overcome this issue?

public String Subject
     get { return this.subject; }
     set { this.subject = value; }

public AppSettings Subject
     get {
         return this.subjectObj; }
     set {
         this.subjectObj = value; }

No, it's not possible. You would not be able to tell these properties apart when using them. How would you know which property is set in this case?

someInstance.Subject = null;

The official documentation on class members states:

The name of a constant, field, property, event, or type must differ from the names of all other members declared in the same class.

Possible solution:

Your naming suggests that you may want to create another class Subject with at least two properties. (Without more context, I can't say if this is appropriate.)

public class Subject
    public string Name { get; set; }
    public AppSettings Settings { get; set; }

This new class could be used in the original class:

public OriginalClass
    public Subject subject { get; set; }