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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:25:59+00:00 2026-06-09T18:25:59+00:00

Say I have a struct defined as such struct Student { int age; int

  • 0

Say I have a struct defined as such

struct Student
{
  int age;
  int height;
  char[] name[12];
}

When I’m reading a binary file, it looks something like

List<Student> students = new List<Student>();
Student someStudent;

int num_students = myFile.readUInt32();
for (int i = 0; i < num_students; i++)
{
   // read a student struct
}

How can I write my struct so that I just need to say something along the lines of

someStudent = new Student();

So that it will read the file in the order that the struct is defined, and allow me to get the values as needed with syntax like

someStudent.age;

I could define the Student as a class and have the constructor read data and populate them, but it wouldn’t have any methods beyond getters/setters so I thought a struct would be more appropriate.

Or does it not matter whether I use a class or struct? I’ve seen others write C code using structs to read in blocks of data and figured it was a “good” way to do it.

  • 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-09T18:26:01+00:00Added an answer on June 9, 2026 at 6:26 pm

    If you want to serialize / deserialize the struct

    If you want to read/write the entire struct to a binary file (serialization), I suggest you look at

    https://stackoverflow.com/a/629120/141172

    Or, if it is an option for you, follow @Marc’s advice and use a cross-platform serializer. Personally I would suggest protobuf-net which just happens to have been written by @Marc.

    If you are loading from an arbitrary file format

    Just like a class, a struct can have a constructor that accepts multiple parameters. In fact, it is generally wise to not provide setters for a struct. Doing so allows the values of the struct to be changed after it is constructed, which generally leads to programming bugs because many developers fail to appreciate the fact that struct is a value type with value semantics.

    I would suggest providing a single constructor to initialize your struct, reading the values from the file into temporary variables, and then constructing the struct with a constructor.

    public stuct MyStruct
    {
        public int Age { get; private set; }
        public int Height { get; private set; }
        private char[] name;
        public char[] Name 
        {
            get { return name; }
            set
            {
                if (value.Length > 12) throw new Exception("Max length is 12");
                name = value;
            }
        }
        public MyStruct(int age, int height, char[] name)
        {
        }
    }
    

    To dig further into the perils of mutable structs (ones that can be changed after initialized) I suggest

    Why are mutable structs “evil”?

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

Sidebar

Related Questions

Say I have an object: struct Foo { int bar_; Foo(int bar) bar_(bar) {}
Say I have a type edge: struct edge { long long weight; int dest;
Say I have the following code: struct date { int day; int month; int
Let's say I have a first structure like this: typedef struct { int ivalue;
Say I have a struct defined somewhere deep in low-level code used all over
Say I have a struct: struct MyStruct { public int X public int Y
Say I have a struct that looks like this (a POD): struct Foo {
Lets say, I have the following text file: Listing 1: Endianess=little AddressModel=32 typedef struct{
Say I have the following C structure definition: struct stringStructure { char *stringVariable; };
Say I have this struct: struct MyStruct { int iID; int iMyNumber; }; Then

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.