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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:44:14+00:00 2026-05-27T00:44:14+00:00

In C++, you can pass into a function an object that is const. This

  • 0

In C++, you can pass into a function an object that is const. This forces the code to treat that object as ‘read only’ (ignoring the fact that you can strip constness away using casts). I’m trying to do the same with C#. I have an interface that the user will implement. One of the methods will be given an object as a parameter. How do I make the object ‘const’ in the scope of the method and mutable otherwise.

Solution: interfaces. I make an interface where the only action available on a property, is ‘get’.

public interface IVal { ... }


public interface IFoo
{
    IVal Val { get; }
}

public class Foo : IFoo
{
    public IVal Val { get; }
}

I pass by interface and everything is amazing. However, in my concrete class, I would like to have a ‘set’ available. It would allow me to set a concrete class Value. How would my concrete class look like?

I was thinking of using the ‘internal’ keyword. I read that it exposes the method/property to code within your own assembly. Is this my answer?

EDIT: To illustrate this better, I added a setter that would automatically cast for me. However, for each property, this is a lot of repetative code:

private Val _val;
public IVal Val
{
    get { return this._val; }
    set { this._val = value as Val; }
}
  • 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-27T00:44:15+00:00Added an answer on May 27, 2026 at 12:44 am

    You can just add the set and it will work fine:

    public IValue Value { get; set; }
    

    Like this, you can set the value if you have Foo, but not if you have IFoo. Similar to the C++ case, if you have IFoo, you can cast to Foo (as long as the object actually is Foo) and then use the setter.

    Another option is to make the setter protected or private:

    public IValue Value { get; private set; }
    

    This allows you to make sure that only code in this class (or inherited classed in the case of protected) can set the property.

    Regarding internal: yes, it exposes the member only to code in the same assembly (and friend assemblies). But this is not used very often. What is used (and useful) are internal types, which is even the default, if you don’t specify that that class is public.

    This could help you with your problem. If you make Foo internal, a method in other assembly that has IFoo won’t be able to set the property, even with casting.

    Of course, the code could always use reflection to set the property. But you shouldn’t worry about that most of the time.

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

Sidebar

Related Questions

Can i pass the entire POST array into a function and handle it within
I've got a chunk of code where I can pass info into a MySQL
You can pass a function pointer, function object (or boost lambda) to std::sort to
I'm trying to write a function that returns an Array that I can pass
I'm attempting to create a function where I can pass names and values that
I have been working on a wrapper for a COM object that can only
Is there a way i can pass information into a nested function ? The
When I pass an anonymous inner class into a function, I can refer to
I am trying to make a function I can pass an mysql query into
How can I return an object with an update data, that is pass in

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.