0

I'm new to C# and I was trying to use set accessor to add a new Person object to a Person List called children in my class Person with the following approach, but also I want to use the get accessor to return an array of Persons.

the problem is that the value field in set accessor is an array of Person and I don't want it to be like that, I want it to be an Instance of Person NOT an array of persons, so I can add it to the List

class Person
    {
        private string firstName;
        private string lastName;

        public string FullName
        {
            get
            {
                return firstName + lastName;
            }
        }

        public readonly DateTime dateOfBirth;

        public readonly FavoriteColor favoriteColor;

        private List<Person> children;

        public Person[] Children
        {
            get
            {
                return children.ToArray();
            }

            set
            {
                children.Add(value);
            }
        }

    }

So is there any thing that I can do to achieve that in C# using properties?

  • You *could* use `object` as the type of the `Children` property, but this will do more harm than doing any favor for you. Why do you want to use a setter as an "adder" property? What do you gain by that? – Progman Mar 12 '21 at 23:21
  • 1
    What you're specifically asking for can't be done in C#, but it sounds like an XY Problem. Can you explain why you want this? Do you expect consuming code to say `person.Children = baby;` instead of `person.Children.Add(baby)`? Are you looking to use initializer syntax? – StriplingWarrior Mar 12 '21 at 23:22
  • Your idea seems like a pretty blatant violation of the [Principle of Least Astonishment](https://softwareengineering.stackexchange.com/questions/187457/what-is-the-principle-of-least-astonishment). Why not write an `AddChild` method instead? – John Wu Mar 12 '21 at 23:28
  • @StriplingWarrior I was just asking if there is a way in C# to change the type of "value" field in set accessor from Person[] to Person rather than using the type I declared the Property Children which is Person[]. – Mahmoud Kanbar Mar 12 '21 at 23:29
  • @JohnWu I knew that I can do this by a method, it's just because I'm new to C# and I want to understand more things about properties – Mahmoud Kanbar Mar 12 '21 at 23:33
  • I understand what you asked. Was that a purely academic question? If so, the answer is, "no, there is not." Often questions like this are driven by a practical need, though. If that's the case, providing more context for that need might help us give you an answer to the underlying question. – StriplingWarrior Mar 12 '21 at 23:34
  • 1
    @StriplingWarrior yes it was an academic question, I'm new to C# and properties were a totally new concept for me in programming – Mahmoud Kanbar Mar 12 '21 at 23:39

3 Answers3

3

It's not possible to have different type for the getter and setter on the same property. Neither would it make much sense, in most cases. You could use a different property for getting and setting:

public Person[] Children => children.ToArray();
public Person LatestChild
{
    set { this.children.Add(value); }
}

But honestly just adding an AddChild method would probably be a better idea.

public Person[] Children => children.ToArray();
public void AddChild(Person child)
{
    children.Add(value);
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
1

If overloading an assignment (single =) was supported, you could perhaps do this.

That said, overloading assignment operators is not supported in the language (here's a few informal references: 1, 2).

What you're doing is not idiomatic of the language, and thus I discourage you from even trying to do this. You're attempting to redefine an operation in a way that won't be intuitive or obvious to other readers of your code.

Instead, provide an explicit method that adds the value to the array (by the way, that means allocating a new array, as arrays have fixed bounds); this makes your intent clear. You could go the route of overloading +, which overloads +=, but this is still getting out of the realm of the language's idioms, and so should probably be discouraged).

Kit
  • 20,354
  • 4
  • 60
  • 103
0

The setter expects an object of type Person[], because that's how you defined the property.

Make the array property read-only by removing the setter and add an Add(Person person) method to your class instead.

Henk van Boeijen
  • 7,357
  • 6
  • 32
  • 42