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 3691500
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:09:18+00:00 2026-05-19T04:09:18+00:00

.NET structs are value types, meaning that if function A creates a struct and

  • 0

.NET structs are value types, meaning that if function A creates a struct and calls function B, which tries to change the struct, B will get a new copy of the struct, so the changes won’t apply to A’s struct.

Structs can be much bigger than the CLR’s other value types. Lets say that one function creates a large struct, calls another and passes the struct to the function. This goes on for 10 levels, but all functions just need to read data from the struct, and do not change any of the fields . If a new copy of the struct is indeed created in each function call, the above scenario will result allocating many unneeded structs.

C language users can avoid this by passing a pointer instead of the struct itself, and if the inner functions should not change that data in the struct, the const keyword can be used.

However, C# has references instead of pointers, and there is no such thing as “const ref”.

My question is: Is .NET optimized in such way that it knows to copy a struct only when a function tries to change the fields inside, or that new copy is always created once a struct is passed to another function?

  • 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-19T04:09:18+00:00Added an answer on May 19, 2026 at 4:09 am

    the above scenario will result allocating many unneeded structs

    Thinking about this in terms of “allocate” is pretty drastic mismatch with what really happens. Struct values are passed through CPU registers, the stack if they get large or there are too many arguments to pass. This is dirt cheap, no runtime support method gets called and there is no notion of “freeing” the allocation.

    This otherwise works almost identically as in native C/C++ code, with the caveat that the x86 JIT compiler tends to generate better code since struct passing always goes through the stack in the native calling conventions (other than __fastcall). Pointers are supported as well, just like in C, you just declare the argument with the ref keyword. Minus the const keyword, you’ll have to live without that one.

    The cost of passing structs goes up drastically when the struct is larger than 16 bytes. It no longer gets pushed on the stack, the jitter generates code to pass a pointer to a copy of the value on the local stack frame. In effect the value gets copied twice, at the call site as well as the callee. Which is why the .NET framework guide recommends switching to a class when the struct gets too large. Passing by ref however works too to avoid the copying, with the caveat that all member accesses are indirect. Just like in C.

    Don’t hesitate to use a class instead of struct, the garbage collector is very efficient.

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

Sidebar

Related Questions

In .NET, a value type (C# struct ) can't have a constructor with no
Can I really and truly trust .NET to initialize fields (like ints, structs and
What's the difference between struct and class in .NET?
.NET newbie here... I'd like to make a button in a Windows form that
Both .NET Socket and NetworkStream offer Read method, which blocks, when there is no
What .Net wrappers are there for Lua? Google wasn't too helpful, although that could
Old .NET Compact Framework apps still run on Windows Phone 7? Will new apps
.NET Framework: 2.0 Preferred Language: C# I am new to TDD (Test Driven Development).
.NET, Java and other high level database API's in various language often provide techniques
.NET developers out there! Need your opinion here! I am now using Visual Assist

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.