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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:23:51+00:00 2026-05-24T20:23:51+00:00

Does a Nullable value type generate garbage? For example: A struct is not created

  • 0

Does a Nullable value type generate garbage? For example: A struct is not created on the heap but on the stack because it’s a value type.
But when that struct is made nullable, is it still a value type and still created on the stack?

I ask this question because I need a nullable struct that does not generate garbage.

  • 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-24T20:23:52+00:00Added an answer on May 24, 2026 at 8:23 pm

    A struct is not created on the heap but on the stack because it’s a value type.

    That is a common misperception, and completely misleading. A struct that is an array element is created on the heap. A struct that is a field of a class is created on the heap. A struct that is a closed-over outer variable of a lambda is created on the heap. Structs are created on the heap all the time. Structs are only created on the stack when their variable lifetimes are known to be shorter than that of the method they’re in. Obviously they cannot be created on the stack if their lifetimes are longer than the stack frame of the method!.

    Also, everyone forgets registers. Registers are neither heap nor stack. Nothing stops the optimizer from generating a struct as a register if the optimizer decides that burning the register is worth it.

    But when that struct is made nullable, is it still a value type?

    Yes. A nullable type is a value type. (Though it does not meet the value type constraint of a generic type or method, and it has special boxing behaviour.)

    Is it still created on the stack?

    If the non-nullable value type would have been created on the heap then the nullable one will be created there too.

    I need a nullable struct that does not generate garbage.

    If the non-nullable struct did not generate garbage then the nullable struct will not either.

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

Sidebar

Related Questions

I'm trying to implement a class that stores a generic nullable type: public class
Please consider the following Amount value type property which is marked as a nullable
Desired behavior An undecorated non-nullable value-type property in a model automatically gets a validation
Possible Duplicates: Why does null need an explicit type cast here? Nullable types and
I have a situation where I have a simple, immutable value type: public struct
Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone use have a good regex library that they like to use? Most
Does anyone know how to transform a enum value to a human readable value?
I'm calling functions in the XNA framework from F# that accept Nullable values. Now,

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.