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

  • Home
  • SEARCH
  • 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 1091539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:32:41+00:00 2026-05-16T23:32:41+00:00

A simple question, but I haven’t found a definitive answer on Stack Overflow. struct

  • 0

A simple question, but I haven’t found a definitive answer on Stack Overflow.

    struct MyStruct { int x, y, z; }

    MyStruct GetMyStruct() => new MyStruct();

    static void Main()
    {
        var x = GetMyStruct();      // can boxing/unboxing ever occur?
    }

Is a C# struct (value type) always copied to the stack when returned from a function, no matter how large it might be? The reason I’m unsure is that for some instruction sets other than MSIL (such as x86), a return value usually needs to fit into a processor register, and the stack is not directly involved.

If so, is it the call site that pre-allocates space on the CLR stack for the (expected) value return type?


[edit: Summary of replies]
For the intent of the original question, the answer is no; the CLR will never (silently) box a struct just for the purpose of transferring it as a return value.

  • 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-16T23:32:42+00:00Added an answer on May 16, 2026 at 11:32 pm

    It is a heavy implementation detail of the JIT compiler. In general, if the struct is small enough and has simple members then its gets returned in CPU registers. If it gets too big then the calling code reserves enough space on the stack and passes a pointer to that space as an extra hidden argument.

    It will never be boxed, unless the return type of the method is object of course.

    Fwiw: this is also the reason that the debugger cannot display the return value of the function in the Autos window. Painful sometimes. But the debugger doesn’t get enough metadata from the JIT compiler to know exactly where to find the value. Edit: fixed in VS2013.

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

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • 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 You could potentially accomplish your goals by using T4. This… May 17, 2026 at 2:33 am
  • Editorial Team
    Editorial Team added an answer The reason is that preprocessor directives don't use semicolons. This… May 17, 2026 at 2:33 am
  • Editorial Team
    Editorial Team added an answer To relate two strings, it's easiest to use an object… May 17, 2026 at 2:33 am

Trending Tags

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

Top Members

Related Questions

One simple question (but I haven't quite found an obvious answer in the NLP
Sounds a simple question but haven't found a way to do, so would solicit
Simple question, but I haven't found a good explanation on google. When using Set
This should be a simple question but I haven't managed to find the answer
Probably a simple question but I haven't found a solution. I have several view
This seems like a really simple question but I haven't seen it answered anywhere.
Quite a simple question, but I haven't been able to find any useful resources
This should be a simple question, but I haven't been able to find a
I am sure this question has been asked and answered; but I haven't managed
This is some very simple java OOP but I haven't done this in a

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.