Search code examples
c#.netobjectvalue-typereference-type

Which is more performant, passing a method the entire object, or a property of that object?


Consider the following example.

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

I have two choices:

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?


Solution

  • I believe the performance will be the same.

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

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