Naming an object that is an attribute of another object with the same getter/setter as Object Type

删除回忆录丶 提交于 2019-12-10 23:29:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!