哪个更高性能,传递的方法,整个对象或对象的属性? [英] Which is more performant, passing a method the entire object, or a property of that object?

查看:235
本文介绍了哪个更高性能,传递的方法,整个对象或对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的例子。

我需要检查,如果一个CouponModel都有一个唯一的序列码。

I need to check if a CouponModel has a unique serial key.

我有两个选择:

CouponModel model = GetFromSomewhere();

if (!CouponHasUniqueKey(model))
{
}

//or

if (!CouponHasUniqueKey(model.SerialKey))
{
}

当然,在我通过在整个对象的方法,我会直接访问与字符串工作的字符串属性。

Of course in the method where I pass in the whole object I would have to access the string property instead of working directly with the string.

哪种选择更好,为什么?

Which option is better and why?

推荐答案

我相信,性能将是相同的。

I believe the performance will be the same.

我也会preFER给责任对象本身:

I'd also prefer to give the responsibility to the object itself:

CouponModel coupon = GetFromSomewhere();
if (!coupon.HasUniqueKey()) {
  // ...
}

这篇关于哪个更高性能,传递的方法,整个对象或对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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