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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:53:16+00:00 2026-05-24T23:53:16+00:00

If an object is readonly or const, is it possible to cast that object

  • 0

If an object is readonly or const, is it possible to cast that object to make it writable?
Something similar to C++ const_cast.

  • 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-24T23:53:16+00:00Added an answer on May 24, 2026 at 11:53 pm

    It’s not possible in C#, just like it’s not possible in C++. In C++, if the object is really const, you cannot const_cast the constness away and write to it without invoking undefined behaviour:

    struct foo { const int x; };
    
    foo a;
    int& b = const_cast<int&>(a.x);
    b = 17; // invokes undefined behaviour
    

    A readonly field in C# only means that the field itself cannot be reassigned. It’s akin to T *const or T& in C++. You can change the referenced object at will through its members.

    class Foo { public int x; }
    class Bar { public readonly Foo y = new Foo(); }
    
    Bar a = new Bar();
    a.y.x = 3; // valid
    a.y = new Foo(); // invalid
    

    Well, I’m not telling the whole truth. You can cheat and change readonly fields through reflection1:

    typeof(string).GetField("Empty").SetValue(null, "bar");
    // this effectively makes string.Empty equal to "bar", with disastrous consequences
    // It requires full trust though.
    // Obviously, it's evil.
    

    If it is a const field however, not even this trick will work.

    const fields are hardcoded in assemblies that use them, instead of keeping references to the original assembly:

    // Assembly A.dll
    public class Foo { public static const int X = 42; }
    
    // Assembly B.dll
    int y = Foo.X;
    // this is the equivalent to:
    int y = 42;
    

    This means that if you recompile A.dll and change the value of Foo.X to 23, B.dll will still use 42 until it is recompiled.

    All that said, if you want to have a field that you want to change, just don’t make it readonly. If you want it to be mutable by the class, but immutable from the outside, make it private and add a read-only property (note: this is not the same as a readonly field):

    class Foo
    {
        private int bar;
        public int Bar
        {
            get { return bar; }
        }
    }
    

    1This is not really guaranteed, but it works on the Microsoft implementations. If you’re wondering why this hack works at all, you can read Eric Lippert’s explanation. Be sure to also read the answer about readonly on value types. And it goes without saying, don’t do this at home.

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

Sidebar

Related Questions

Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Object slicing is some thing that object looses some of its attributes or functions
Since Object Initializers are very similar to JSON, and now there are Anonymous Types
See the code below - I am trying to put a const object into
I have a graph object, that I need to serialize. Nomatter which members i
Assume that I want to perform parallel computations on a large fixed object, e.g.
By default, the QAbstractTableModel class has a mimeData() function that returns a QMimeData object
I want to execute a read-only method on an object marked as const ,
I have a custom object that I am trying to bind to a control.
Object o = new Long[0] System.out.println( o.getClass().isArray() ) System.out.println( o.getClass().getName() ) Class ofArray =

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.