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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:23:18+00:00 2026-06-14T18:23:18+00:00

So when designing an immutable class should it use get properties as such public

  • 0

So when designing an immutable class should it use get properties as such

public sealed class Person
{
    readonly string name;
    readonly int age;

    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public string Name
    {
        get { return name; }
    }

    public int Age
    {
        get { return age; }
    }
}

Or is it valid to expose public readonly fields

public sealed class Person
{
    public readonly string Name;
    public readonly int Age;

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}
  • 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-14T18:23:19+00:00Added an answer on June 14, 2026 at 6:23 pm

    While both options will leave you with objects whose fields cannot be changed from the outside, the same guidelines apply for immutable objects as otherwise:

    Using properties instead of publicly exposing your fields is still an advantage; you are not checking any input values, but still, future versions of your type might compute some of the values rather than read them directly from an internal field. By using properties, you hide that implementation detail and hence increase your flexibility for future changes.

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

Sidebar

Related Questions

Why does the following throw a compiler error: class A { public: int f()
When designing a class, should logic to maintain valid state be incorporated in the
In designing the HTML and CSS for a page, when should I use img.className
I am designing some immutable classes but I have to have some variables like
I have an class defining an immutable value type that I now need to
I designing a polynomial class for one of my com sci courses , I
I have an abstract immutable base class that defines enforces child classes to be
When designing a class that has a reference to another object it might be
I made my class immutable by following all java standards A. Defined class as
When I'm designing immutable objects, case classes are incredibly handy because of the auto-generated

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.