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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:01:31+00:00 2026-05-17T02:01:31+00:00

Coming from a C++ background, I’m used to sticking the const keyword into function

  • 0

Coming from a C++ background, I’m used to sticking the const keyword into function definitions to make objects being passed in read-only values. However, I’ve found out that this is not possible in C# (please correct me if I’m wrong). After some Googling, I arrived at the conclusion that the only way to make a read-only object is to write an interface that only has ‘get’ properties and pass that in instead. Elegant, I must say.

public interface IFoo
{
  IMyValInterface MyVal{ get; }
}

public class Foo : IFoo
{
  private ConcreteMyVal _myVal;

  public IMyValInterface MyVal
  {
    get { return _myVal; }
  }
}

I would pass it into:

public void SomeFunction(IFoo fooVar)
{
  // Cannot modify fooVar, Excellent!!
}

This is fine. However, in the rest of my code, I would like to modify my object normally. Adding a ‘set’ property to the interface would break my read-only restriction. I can add a ‘set’ property to Foo (and not IFoo), but the signature expects an interface rather than a concrete object. I would have to do some casting.

// Add this to class Foo. Might assign null if cast fails??
set { _myVal = value as ConcreteMyVal; }

// Somewhere else in the code...
IFoo myFoo = new Foo;
(myFoo as Foo).MyFoo = new ConcreteMyVal();

Is there a more elegant way of replicating const or making read-only function parameters without adding another property or a function?

  • 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-17T02:01:31+00:00Added an answer on May 17, 2026 at 2:01 am

    I think you may be looking for a solution involving two interfaces in which one inherits from the other:

    public interface IReadableFoo
    {
        IMyValInterface MyVal { get; }
    }
    
    public interface IWritableFoo : IReadableFoo
    {
        IMyValInterface MyVal { set; }
    }
    
    public class Foo : IWritableFoo 
    {
        private ConcreteMyVal _myVal;
    
        public IMyValInterface MyVal
        {
            get { return _myVal; }
            set { _myVal = value as ConcreteMyVal; }
        }
    }
    

    Then you can declare methods whose parameter type “tells” whether it plans on changing the variable or not:

    public void SomeFunction(IReadableFoo fooVar)
    {
        // Cannot modify fooVar, excellent!
    }
    
    public void SomeOtherFunction(IWritableFoo fooVar)
    {
        // Can modify fooVar, take care!
    }
    

    This mimics compile-time checks similar to constness in C++. As Eric Lippert correctly pointed out, this is not the same as immutability. But as a C++ programmer I think you know that.

    By the way, you can achieve slightly better compile-time checking if you declare the type of the property in the class as ConcreteMyVal and implement the interface properties separately:

    public class Foo : IWritableFoo 
    {
        private ConcreteMyVal _myVal;
    
        public ConcreteMyVal MyVal
        {
            get { return _myVal; }
            set { _myVal = value; }
        }
    
        public IMyValInterface IReadableFoo.MyVal { get { return MyVal; } }
        public IMyValInterface IWritableFoo.MyVal
        {
            // (or use “(ConcreteMyVal)value” if you want it to throw
            set { MyVal = value as ConcreteMyVal; }
        }
    }
    

    This way, the setter can only throw when accessed through the interface, but not when accessed through the class.

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

Sidebar

Related Questions

I know its probably possible, but is it practical and doable to try and
I know its probably possible, but is it practical and doable to try and

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.