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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:43:21+00:00 2026-06-18T11:43:21+00:00

How do I write a property for a list inside of a struct? my

  • 0

How do I write a property for a list inside of a struct?

my code:

public struct Config
{
    List<int> ipAddress = new List<int>();
}
  • 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-18T11:43:22+00:00Added an answer on June 18, 2026 at 11:43 am

    If all you want is to create an auto-property (which will default to null for reference types and cannot be initialized) you can do as @scartag suggests.

    public struct Config
    {
        // will default to null
        List<int> ipAddress {get; set;}
    }
    

    However, if you’re trying to do what’s in your code and initialize it to an actual reference, you will run into issues because you can’t initialize fields in a struct. They can only have their defaults. To make matters even worse, you can’t override a default constructor to do this for you.

    Generally speaking, struct tends to be best for small, preferably immutable types. Is there a reason you don’t want to just use class for this?

    Now, if you did want to create a struct with an “initialized” field, you can fool it with some lazy logic:

    public struct Config
    {
        private List<int> _ipAddress;
        private bool _isAssigned;
    
        public List<int> 
        {
            get 
            {
                if (!_isAssigned)
                    _ipAddress = new List<int>;
                return _ipAddress; 
            }
            set
            {
                _ipAddress = value;
                _isAssigned = true;
            }
        }
    }
    

    But really, at this rate it’s better to use a class since you can initialize fields, or override the default constructor:

    public class Config
    {
        public List<int> ipAddress {get; set;}
    
        public Config()
        {
            ipAddress = new List<int>();
        }
    }
    

    And again, as Andrew mentioned in the comments, I strongly suggest looking at MSDN guidance on choosing between struct and class

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

Sidebar

Related Questions

I created a property list with the name propertys.plist. Then I wrote this: NSString
How can I write a unit test to test the ActualWidth property in a
I wrote the following sample code to see how ARC works @property (nonatomic, weak)
I am trying to write a Linq expression that checks against property in a
In the application that I am writing I am trying to write a list
How does the indexing for repeated/list property work, I read somewhere that adding to
I'm trying to write a simple property grid to allow the users to modify
I am currently using the following code to access the property of an object
I'm trying to write a function which checks if every element in the list
I am getting a list of items in my reader. There is a property

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.