揭会员对象作为属性或方法在.NET属性、对象、方法、会员

2023-09-02 21:29:25 作者:我不是2B我是铅笔

在.NET中,如果一个类包含一个成员是一类对象,应该成员可以公开为属性或方法?

In .NET, if a class contains a member that is a class object, should that member be exposed as a property or with a method?

推荐答案

如果你正在做的是暴露一个对象实例,是有关你应该使用属性的当前对象的状态。

If all you are doing is exposing an object instance that is relevant to the state of the current object you should use a property.

当你有一些逻辑是做多的内存对象访问一个并返回一个值时,或者正在执行,有一个对当前对象的状态广泛影响的操作方法应该被使用。

A method should be used when you have some logic that is doing more than accessing an in memory object and returning that value or when you are performing an action that has a broad affect on the state of the current object.