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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:43:03+00:00 2026-05-26T17:43:03+00:00

The C# readonly keyword is a modifier that when a field declaration includes it,

  • 0

The C# “readonly” keyword is a modifier that when a field declaration includes it, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

Now suppose I do want this “assign value once” constraint, but I would rather allow the assignment be done outside of constructors, a lazy/late evaluation/initialization maybe.

How could I do that? and is it possible to do it in a nice way, for example, is it possible to write some attribute to describe 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-26T17:43:03+00:00Added an answer on May 26, 2026 at 5:43 pm

    Now suppose I do want this “assign value once” constraint, but I would rather allow the assignment be done outside of constructors

    Note that lazy initialization is complicated, so for all of these answers you should be careful if you have multiple threads trying to access your object.

    If you want to do this inside the class

    You can use the C# 4.0 built-in lazy initialization features:

    • http://msdn.microsoft.com/en-us/library/dd997286.aspx
    • http://msdn.microsoft.com/en-us/library/dd642331.aspx
    • http://sankarsan.wordpress.com/2009/10/04/laziness-in-c-4-0-lazyt/

    Or for older versions of C#, just supply a get method, and check if you’re already initialized by using a backing field:

    public string SomeValue
    {
        get
        {
            // Note: Not thread safe...
            if(someValue == null)
            {
                someValue = InitializeSomeValue(); // Todo: Implement
            }
    
            return someValue;
        }
    }
    

    If you want to do this outside the class

    You want Popsicle Immutability:

    • http://blogs.msdn.com/b/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx
    • http://msdn.microsoft.com/en-us/library/ms750509.aspx
    • http://csharpindepth.com/Talks.aspx (search for “popsicle immutability” and you’ll find a video)

    Basically:

    • You make the whole class writable, but add a Freeze method.
    • Once this freeze method is called, if users try to call setters or mutator methods on your class, you throw a ModifyFrozenObjectException.
    • You probably want a way for external classes to determine if your class IsFrozen.

    BTW, I made up these names just now. My selections are admittedly poor, but there is no generically followed convention for this yet.

    For now I’d recommend you create an IFreezable interface, and possibly related exceptions, so you don’t have to depend on the WPF implementation. Something like:

    public interface IFreezable
    {
        void Freeze();
        bool IsFrozen { get; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I make a Property ReadOnly outside the Assembly (DLL) for people using
I'm running string.Format on a readonly string that contains a bit of HTML +
How can I programatically remove the readonly attribute from a directory in C#?
first i must assume that i'm not very familiar with the C# yield keyword
In C++, you can pass into a function an object that is const. This
In C# you can do this to make your member variable immutable: public readonly
I have a custom class that implements ICollection , and this class is readonly,
can we use readonly to modify property? If not, why?
What's the C++/CLI equivalent of C#'s readonly keyword? Specifically, how do you write a
I have a class A that is inherited from B. A as some readonly

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.