传递对象的最佳实践 [英] Best practice for passing object

查看:86
本文介绍了传递对象的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点点简单的问题。

I have a little simple question.

让我们说我有大约10属性的数据对象,我想从我的对象传递数据的功能。
大部分时间我只需要在接收功能,这些价值观之一,也可以同样仅通过该值(比方说一个int)。那么,什么是利弊与始终发送整个对象VS仅发送包含的值之一吗?

Let's say I have a data object with about 10 properties, and I want to pass data from my object to a function. Most of the time I only need one of these values in the receiving function, and could just as well pass only that value (let's say an int). So what is the pros and cons with always sending the whole object vs only sending one of the contained values?

有一个最好的做法?

推荐答案

在传递一个对象时,实际上是刚好路过的参考的对象 - 所以4个字节在x86和8个字节在x64。因此,如果该方法感觉自然采取的对象,只是传递对象;没关系。任何其他的方法可能会是的糟糕

When you "pass an object", you are actually just passing the reference to the object - so 4 bytes on x86 and 8 bytes on x64. So if the method feels natural to take the object, just pass the object; that's fine. Any other approach is probably going to be worse.

当然,如果觉得自然,该方法只需要一个单一的 INT ,只能曾经需要单 INT ,然后传递一个 INT 细的

Of course, if it feels "natural" that the method only needs a single int, and could only ever need the single int, then passing an int is fine too.

我的观点是:编写方法来支持一下有意义的方法

My point is: write the method to support what makes sense for the method.

这篇关于传递对象的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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