At http://blogs.msdn.com/ericgu/archive/2004/01/29/64717.aspx, we learn that C# will not inline methods with structs as formal parameters. Is this due to potential dependence on the stack, e.g. for recursion? If so, could I potentially benefit by turning struct parameters into ref parameters like this?
public int Sum(int i)
{
return array1[i] + array2[i];
}
turns into:
public int Sum(ref int i)
{
return array1[i] + array2[i];
}
Edit: I went to attempt a test, but I can’t get anything in inline. Here is what I tried:
class Program
{
private static string result;
static void Main(string[] args)
{
Console.WriteLine(MethodBase.GetCurrentMethod().Name);
Console.WriteLine();
m1();
Console.WriteLine(result);
}
private static void m1()
{
result = MethodBase.GetCurrentMethod().Name;
}
}
It prints “m1” as the second line, which indicates that it did not get inlined. I built a Release build and ran it with Ctrl-F5 (to not attach the debugger). Any ideas?
As Jon said, it’s a very old post. I can confirm that in the following code:
inlinetestmethod is inlined.Main method disassembly:
I’ve tested this on x86 .NET Framework 3.5 SP1 on Windows 7 x64 RC.
As I believed there’s nothing inherently wrong with inlining methods with
structparameters. Probably, JIT has not been smart enough at that time.