views:

233

answers:

1

I am not expecting a definite yes or no. Any knowledge you might have I will consider as an answer.

private String CalculateCharge(Nullable<Decimal> bill, Nullable<Decimal> rate)
{
    return ((bill ?? 0.0m) * (rate ?? 0.0m)).ToString("C");
}
+14  A: 

Inlining is an implementation detail of the JIT, not of the C# compiler. From Eric Gunnerson's blog:

The JIT uses a number of heuristics to decide whether a method should be in-lined. The following is a list of the more significant of those (note that this is not exhaustive):

  • Methods that are greater than 32 bytes of IL will not be inlined.
  • Virtual functions are not inlined.
  • Methods that have complex flow control will not be in-lined. Complex flow control is any flow control other than if/then/else; in this case, switch or while.
  • Methods that contain exception-handling blocks are not inlined, though methods that throw exceptions are still candidates for inlining.
  • If any of the method's formal arguments are structs, the method will not be inlined.

Although your method is quite short and not very complex so it might match the heuristics, Nullable<T> is a struct so I'd guess your method is not inlined.

As a rule of thumb, if inlining this method improves performance, the JIT will inline this method; otherwise it will not. But this is really an implementation detail of the JIT and nothing you should code for:

I would carefully consider explicitly coding for these heuristics because they might change in future versions of the JIT. Don't compromise the correctness of the method to attempt to guarantee that it will be inlined.

EDIT: Apparently the bit about structs not being inlined is out-of-date; updated information can be found at Vance Morrison's blog.

dtb
The struct option is no longer true as of .NET 3.5sp1. struct parameters can be inlined now.
Reed Copsey
@Reed Copsey: Thanks for the info. Point proven :)
dtb