哪个更高效,传递一个方法整个对象,或该对象的属性?

Onl*_*ere 4 .net c# object value-type reference-type

请考虑以下示例.

我需要检查CouponModel是否有唯一的串行密钥.

我有两个选择:

CouponModel model = GetFromSomewhere();

if (!CouponHasUniqueKey(model))
{
}

//or

if (!CouponHasUniqueKey(model.SerialKey))
{
}
Run Code Online (Sandbox Code Playgroud)

当然,在我传入整个对象的方法中,我必须访问字符串属性,而不是直接使用字符串.

哪个选项更好,为什么?

Jor*_*dão 5

我相信表现会一样.

我也更愿意对对象本身负责:

CouponModel coupon = GetFromSomewhere();
if (!coupon.HasUniqueKey()) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)