问题
This is a fundamental question. When using objects as an attribute for an object, So Object2 has an attribute Object1.
Can you use the Object name as the type and name for the getter and setter or the second object.
Class Object1{}
Class Object2
{
Object1 Object1 {get; set;}
// OR
Object1 Object1_{get; set;}
}
As I've tried it using the same name for simple programs and it works (though I've not dare to venture into complex code with it). I understand that there could be ambiguity, but will this actually work for any project?
回答1:
Yes, you can do it. In fact this is a very common pattern in C#
class Foo
{
Bar Bar {get; set; }
}
来源:https://stackoverflow.com/questions/33071498/naming-an-object-that-is-an-attribute-of-another-object-with-the-same-getter-set