在 .NET 中将成员对象作为属性或方法公开 [英] Exposing Member Objects As Properties or Methods in .NET

查看:15
本文介绍了在 .NET 中将成员对象作为属性或方法公开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .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.

这篇关于在 .NET 中将成员对象作为属性或方法公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆