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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:08:14+00:00 2026-06-09T16:08:14+00:00

I want to make my class immutable. Obvious way would be to declare all

  • 0

I want to make my class immutable. Obvious way would be to declare all fields as get; private set; and to initialize all fields in constructor. So clients must provide everything in constructor. The problem is that when there are ~10 or more fields passing them in constructor become very unreadable, because there are no labels for each field.

For example this is pretty readable:

info = new StockInfo
        {
            Name = data[0] as string,
            Status = s,
            LotSize = (int)data[1],
            ISIN = data[2] as string,
            MinStep = (decimal)data[3]
        };

compare to this:

new StockInfo(data[0] as string, s, (int) data[1], data[2] as string, (decimal) data[3])

And now imaging that I have 10 or more parameters.

So how can I make class immutable saving readability?

I can suggest only use the same formatting when using constructor:

info = new StockInfo(
            data[0] as string,           // Name
            s,                           // Status
            (int)data[1],                // LotSize
            data[2] as string,           // ISIN
            (decimal)data[3]             // MinStep
       );

Can you suggest something better?

  • 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-09T16:08:15+00:00Added an answer on June 9, 2026 at 4:08 pm

    No, it is not possible. Either you have immutable object or you want to have ability to modify object.

    1. You can use named parameters.
    2. You may consider passing other objects (and group parameters), so that one object will contain only parameters that are somehow very similar.

    Looking at your code I may also suggest that you extract parameters first, so instead of passing something like data[0] as string you use string stockName = data[0] as string; and then use stockName. That should make your code more readable.

    If you’re passing so many parameters to the constructor of your object it may be a good idea to revise your design. You may be violating Single Responsibility principle.

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

Sidebar

Related Questions

I want to make an iostream type class. I would like to find the
I want to make a class in java that is accessible to all other
I want to make a java class that would print out: The product of
I want to make my collection immutable outside of my class. from this: public:
I want to make a class in python, which would store a tree.Keeps taking
I want to make a class that will have a single get template method
Lets say I want to make a class myClass with two slots A and
I have style sheet with a class name changebackgroundcolor i want make change in
I don't want to make a new instance of Form1 in the class, and
I have a member variable that in a class that I want to make

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.