Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 261389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:25:40+00:00 2026-05-11T22:25:40+00:00

At http://blogs.msdn.com/ericgu/archive/2004/01/29/64717.aspx , we learn that C# will not inline methods with structs as

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:25:40+00:00Added an answer on May 11, 2026 at 10:25 pm

    As Jon said, it’s a very old post. I can confirm that in the following code:

    using System;
    using System.Runtime.CompilerServices;
    
    struct MyStruct
    {
       public MyStruct(int p)
       {
          X = p;
       }
       public int X;
    
       // prevents optimization of the whole thing to a constant.
       [MethodImpl(MethodImplOptions.NoInlining)]
       static int GetSomeNumber()
       {
           return new Random().Next();
       }
    
       static void Main(string[] args)
       {
          MyStruct x = new MyStruct(GetSomeNumber());
          // the following line is to prevent further optimization:
          for (int i = inlinetest(x); i != 100 ; i /= 2) ; 
       }
    
       static int inlinetest(MyStruct x)
       {
          return x.X + 1;
       }
    }
    

    inlinetest method is inlined.

    Main method disassembly:

    ; set up the stack frame:
    00000000  push        ebp
    00000001  mov         ebp,esp 
    
    ; calls GetSomeNumber:
    00000003  call        dword ptr ds:[005132D8h] 
    
    ; inlined function:
    00000009  inc         eax  
    
    ; the dummy for loop:
    0000000a  cmp         eax,64h 
    0000000d  je          0000001B 
    0000000f  sar         eax,1 
    00000011  jns         00000016 
    00000013  adc         eax,0 
    00000016  cmp         eax,64h 
    00000019  jne         0000000F 
    0000001b  pop         ebp  
    0000001c  ret 
    

    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 struct parameters. Probably, JIT has not been smart enough at that time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've just replicated the same thing on Reddot and have… May 12, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer One very short solution, but maybe not the best, is… May 12, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer Don't do anything special, because the implementation does it for… May 12, 2026 at 12:53 am

Related Questions

I would like to write an application that will copy MP3 files to a
I downloaded the VS2010 CTP from here: http://www.microsoft.com/downloads/details.aspx?FamilyId=922B4655-93D0-4476-BDA4-94CF5F8D4814&displaylang=en It took a while to download
This blog http://blogs.msdn.com/sqlserverstorageengine/archive/2009/01/04/managing-tempdb-in-sql-server-tempdb-configuration.aspx states that it is a good idea to Spread TempDB across
I like the simplistic look and design of some of the Microsoft blogs. Alas,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.