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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:06:54+00:00 2026-05-29T12:06:54+00:00

Let me explain my question by posing a hypothetical situation. Lets start with a

  • 0

Let me explain my question by posing a hypothetical situation. Lets start with a class:

public class PaymentDetails
{
    public int Id {get;set;}
    public string Status {get;set;}
}

And then I have another class:

public class PaymentHelper
{
    private PaymentDetails _paymentDetails;
    public PaymentDetails MyPaymentDetails{ get { return _paymentDetails; } }

    public PaymentHelper()
    {
        _paymentDetails = new PaymentDetails(); 
    }

    public void ModifyPaymentDetails(string someString)
    {
        // code to take the arguments and modify this._paymentDetails
    }
}

OK, so I have these two classes. PaymentHelper has made the property MyPaymentDetails read-only.

So I cannot instantiate PaymentHelper and modify MyPaymentDetails like this:

PaymentHelper ph = new PaymentHelper();
ph.MyPaymentDetails = new PaymentDetails(); // Not allowed!!! 

But I can modify the public properties inside of ph.MyPaymentDetails like this:

ph.MyPaymentDetails.Status = "Some status"; // This is allowed

How do I prevent that from working? Or is there no good way of doing that?

  • 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-29T12:06:55+00:00Added an answer on May 29, 2026 at 12:06 pm

    The idea of protecting the properties of a complex type that is itself a property isn’t available from a language construct at that level.

    One option is to design the contained type in such a way as to make its properties read-only using the access modifiers (public set, protected set, private set, etc).

    My preference is to expose it as an interface to public consumers:

    public class PaymentHelper
    {
        private PaymentDetails _paymentDetails;
        public IPaymentDetails MyPaymentDetails{ get { return _paymentDetails; } }
    
        public PaymentHelper()
        {
            _paymentDetails = new PaymentDetails(); 
        }
    
        public void ModifyPaymentDetails(string someString)
        {
            // code to take the arguments and modify this._paymentDetails
        }
    }
    
    interface IPaymentDetails
    {
       int Status { get; }
    }
    

    Code inside the PaymentHelper class can then use the PaymentDetails class directly, and code outside the class won’t be able to use PaymentDetails unless they cast directly to it, which you can stop if you don’t release the PaymentDetails class and only provide the interface.

    Of course, you can never really stop the determined person who may use reflection to set things. I tend to let these people break the code 🙂

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

Sidebar

Related Questions

Let me use the following example to explain my question: public string ExampleFunction(string Variable)
Before you start laughing at such a simple question let me explain: I am
let me explain my current situation i have a SharePoint site lets say it
Let me explain the situation before I ask the question. I have a site,
Let me explain what I mean with that question: Lets say I have to
Before I get into to my question,let me explain what I am exactly doing.I
My question might not be understandable enough, so let me explain the situation :
Sorry if the question title is confusing. Let me explain further. I am building
This question might not seem programming related at first, but let me explain. I'm
I know the question title isn't the best. Let me explain. I do a

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.