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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:15:07+00:00 2026-05-13T20:15:07+00:00

I am reading existing code. I noticed that there are many data object files

  • 0

I am reading existing code. I noticed that there are many data object files which have a struct and a class together to define a data object. Like the following one: do you think it is a good style?

In ONE file:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct LaneDataStruct
    {
        public ushort red;
        public ushort yellow;
        public ushort green;
        public ushort blue;
        public ushort orange;
    }

public class LaneData
    {
        private LaneDataStruct laneDataStruct;
    public LaneData(ushort red, ushort yellow, ushort green, ushort blue, ushort orange)
    {
        this.laneDataStruct.red = red;
        this.laneDataStruct.yellow = yellow;
        this.laneDataStruct.green = green;
        this.laneDataStruct.blue = blue;
        this.laneDataStruct.orange = orange;
    }

    public LaneData(ushort[] values)
    {
        this.laneDataStruct.red = values[0];
        this.laneDataStruct.yellow = values[1];
        this.laneDataStruct.green = values[2];
        this.laneDataStruct.blue = values[3];
        this.laneDataStruct.orange = values[4];
    }

    public LaneData(LaneDataStruct laneDataStruct)
    {
        this.laneDataStruct = laneDataStruct;
    }

    public LaneDataStruct getLaneDataStruct()
    {
        return this.laneDataStruct;
    }

    public string toString()
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.Append("LaneData.red=" + this.getLaneDataStruct().red + "\n");
        stringBuilder.Append("LaneData.yellow=" + this.getLaneDataStruct().yellow + "\n");
        stringBuilder.Append("LaneData.green=" + this.getLaneDataStruct().green + "\n");
        stringBuilder.Append("LaneData.blue=" + this.getLaneDataStruct().blue + "\n");
        stringBuilder.Append("LaneData.orange=" + this.getLaneDataStruct().orange);

        return stringBuilder.ToString();
    }
}
  • 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-13T20:15:08+00:00Added an answer on May 13, 2026 at 8:15 pm

    I would only call this a good practice in an exotic P/Invoke scenario, and even then it’s questionable.

    First, the struct is poorly designed since it is mutable. See this question for more information.

    The class looks like it was written to address the fact that in C#, there is no deterministic way to initialize a structure. You can’t give a struct a default constructor, and you can’t cause a non-default constructor to be called. So it looks like the class wraps the structure and gives a guarantee that the structure will be initialized to some state. However, it does no validation so I strongly doubt there is any value to it at all.

    This would only be “useful” if LaneDataStruct represents a structure that is used for P/Invoke (the LayoutKind attribute is a hint this may be true) and that can’t have certain values. It would still be preferable to give it properties that perform validation and make the fields private. Usually, P/Invoke code is written in a separate layer where it’s considered acceptable to have warts like “make sure to initialize this struct after creating one”.

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

Sidebar

Related Questions

is there a C#/.NET library or otherwise existing code that can read persisted data
Reading existing code at work, I wondered how come this could work. I have
I'm trying to update some existing code that normally runs a Perl script which
When reading data from the Input file I noticed that the ¥ symbom was
I have a very simple managed bean LOB which is used for reading existing
I am currently reading an existing code in MS Visual C++ 6.0. I notice
We have an existing Perl application that supports mod_perl . However, our new host
I have an existing site that I'd like to convert to use routing, and
There are some articles which indicate that async database calls are bad idea in
I'm testing my existing code on PHP5.4 prior to upgrade. I've discovered that the

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.