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

  • Home
  • SEARCH
  • 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 6471357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:12:54+00:00 2026-05-25T06:12:54+00:00

When coding C# I often find myself implementing immutable types. I always end up

  • 0

When coding C# I often find myself implementing immutable types.
I always end up writing quite a lot of code and I am wondering whether there is a faster way to achieve it.

What I normally write:

public struct MyType
{
  private Int32 _value;
  public Int32 Value { get { return _value;} }

  public MyType(Int32 val)
  {
     _value = val;
  }
}

MyType alpha = new MyType(42);

This gets fairly complicated when the number of fields grows and it is a lot of typing.
Is there a more efficient way for doing this?

  • 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-25T06:12:54+00:00Added an answer on May 25, 2026 at 6:12 am

    The only way I can suggest of writing less code is to use something like ReSharper to auto-generate the code for you. If you start with something like:

    public class MyType
    {
        private int _value;
    }
    

    you can then generate “read-only properties” to give:

    public class MyType
    {
        private int _value;
        public int Value{get {return _value;}}
    }
    

    followed by generate constructor to give:

    public class MyType
    {
        private int _value;
        public int Value{get {return _value;}}
    
        public MyType(int value)
        {
            _value = value;
        }
    }
    

    The generation steps are 8 key presses in total.


    If you really want an unmodifiable immutable class, I would declare it as such:

    public sealed class MyType
    {
        public int Value{get {return _value;}}
        private readonly int _value;
    
        public MyType(int value)
        {
            _value = value;
        }
    }
    

    This makes the class non-derivable (meaning that a sub-class cannot modify its inner state), and the _value property assignable only during construction. Unfortunately, ReSharper doesn’t have code generation for this pattern, so you would still have to construct (most of) it manually.

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

Sidebar

Related Questions

As a UI guy (coding and designing user interfaces) I often find myself in
Often I find myself coming across code like this: try { StreamWriter strw =
I find myself adding debugging print statements quite often -- stuff like this: print(a_variable_name:
I used to often find myself coping a piece of code from a website/Word
I often find myself wanting to open Xcode and do a little coding on
This is my first time on StackOverflow though I read Coding Horror quite often.
In my java coding, I often end up with several Map<String,Map<String,foo>> or Map<String,List<String>> and
I myself am one of these types of coders, who never learned coding in
I often find myself using several programming languages when making .NET applications. Usually C++/CLI
I often find myself using inline initialization (see example below), especially in a switch

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.