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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:39:44+00:00 2026-06-14T10:39:44+00:00

I ran into this issue today when creating a struct to hold a bunch

  • 0

I ran into this issue today when creating a struct to hold a bunch of data. Here is an example:

public struct ExampleStruct
{
    public int Value { get; private set; }

    public ExampleStruct(int value = 1)
        : this()
    {
        Value = value;
    }
}

Looks fine and dandy. The problem is when I try to use this constructor without specifying a value and desiring to use the defaulted value of 1 for the parameter:

private static void Main(string[] args)
{
    ExampleStruct example1 = new ExampleStruct();

    Console.WriteLine(example1.Value);
}

This code outputs 0 and does not output 1. The reason is that all structs have public parameter-less constructors. So, like how I’m calling this() my explicit constructor, in Main, that same thing occurs where new ExampleStruct() is actually calling ExampleStruct() but not calling ExampleStruct(int value = 1). Since it does that, it uses int‘s default value of 0 as Value.

To make matters worse, my actual code is checking to see that int value = 1 parameter is within a valid range within the constructor. Add this to the ExampleStruct(int value = 1) constructor above:

if(value < 1 || value > 3)
{
    throw new ArgumentException("Value is out of range");
}

So, as it stands right now, the default constructor actually created an object that is invalid in the context I need it for. Anyone know how I can either:

  • A. Call the ExampleStruct(int value = 1) constructor.
  • B. Modify how the default values are populated for the ExampleStruct() constructor.
  • C. Some other suggestion/option.

Also, I am aware that I could use a field like this instead of my Value property:

public readonly int Value;

But my philosophy is to use fields privately unless they are const or static.

Lastly, the reason I’m using a struct instead of a class is because this is simply an object to hold non-mutable data, should be fully populated when it is constructed, and when passed as a parameter, should not be able to be null (since it is passed by value as a struct), which is what struct’s are designed for.

  • 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-06-14T10:39:46+00:00Added an answer on June 14, 2026 at 10:39 am

    Actually, MSDN has some good guidance on struct

    Consider defining a structure instead of a class if instances of the
    type are small and commonly short-lived or are commonly embedded in
    other objects.

    Do not define a structure unless the type has all of the following
    characteristics:

    It logically represents a single value, similar to primitive types
    (integer, double, and so on).

    It has an instance size smaller than 16 bytes.

    It is immutable.

    It will not have to be boxed frequently.

    Notice that they are considerations for considering a struct, and its never a “this should always be a struct”. That is because the choice to use a struct can have performance and usage implications (both positive and negative) and should be chosen carefully.

    Notice in particular that they don’t recommend struct for things > 16 bytes (then the cost of copying becomes more expensive than copying a reference).

    Now, for your case, there is really no good way to do this other than to create a factory to generate a struct for you in a default state or to do some sort of trick in your property to fool it into initializing on first use.

    Remember, a struct is supposed to work such that new X() == default(X), that is, a newly constructed struct will contain the default values for all fields of that struct. This is pretty evident, since C# will not let you define a parameterless constructor for a struct, though it is curious that they allow all arguments to be defaulted without a warning.

    Thus, I’d actually suggest you stick with a class and make it immutable and just check for null on the methods that it gets passed to.

    public class ExampleClass
    {
        // have property expose the "default" if not yet set
        public int Value { get; private set; }
    
        // remove default, doesn't work
        public ExampleStruct(int value)
        {
            Value = value;
        }
    }
    

    However, if you absolutely must have a struct for other reasons – but please consider the costs of struct such as copy-casts, etc – you could do this:

    public struct ExampleStruct
    {
        private int? _value;
    
        // have property expose the "default" if not yet set
        public int Value
        {
            get { return _value ?? 1; }
        }
    
        // remove default, doesn't work
        public ExampleStruct(int value)
            : this()
        {
            _value = value;
        }
    }
    

    Notice that by default, the Nullable<int> will be null (that is, HasValue == false), thus if this is true, we didn’t set it yet, and can use the null-coalescing operator to return our default of 1 instead. If we did set it in the constructor, it will be non-null and take that value instead…

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

Sidebar

Related Questions

I ran into a interesting issue today. Check out this pseudo-code: void Loop() {
I ran into this really frustrating issue today. I want to set my view
Ran into this problem today, posting in case someone else has the same issue.
I ran into a funny issue today working with large data structures. I initially
I ran into an issue using a struct today that caught me off guard,
I ran into this problem today, haven't been having an issue. Any suggestions where
I ran into this interesting issue today. I have a button on a wpf
I ran into this issue while testing a rails app deployed to two different
I just ran into this issue while making a GET request to a node.js
I've ran into trouble with SOAP, I've never had this issue before and can't

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.