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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:36:45+00:00 2026-06-02T08:36:45+00:00

Is there a way to define a field as private but still modifiable within

  • 0

Is there a way to define a field as private but still modifiable within a method using the generic of the containing class?

(Apologies for the length of the question; I am struggling to nail down exactly what I am asking)

Specifically, I currently have three classes A, B, C with the following requirements:

  1. B and C have a field called name, but A does not
  2. A and C have a method Load(), but B does not
  3. Collections of B and C use name in a custom Add() method, but A does not use this method

This is my current solution:

public class BaseClass
{
    public SomeType Load() { ... }
}

public interface IClass {
    string Name {get; set;}
}

public class MyList : SomeCollection<IClass>
{
    // Depends on x.Name, and may modify Name before adding x to the collection
    public new void Add(IClass x) { ... }
}

public class A : BaseClass { ... }

public class B : IClass { ... }

public class C : BaseClass, IClass { ... }

This solution works for me if Name has a public set. But is there some way to properly encapsulate x.Name so that Add can modify x.Name, but x.Name cannot be modified in instances of B and C?

  • 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-06-02T08:36:47+00:00Added an answer on June 2, 2026 at 8:36 am

    The short answer is no. You cannot selectively expose members to specific classes, and MyList has no inheritance relationship with IClass.
    What you can do, however, is create an additional internal interface (or possibly make IClass internal) that exposes the Name property and implement it explicitly. This means that in order to read/write the Name property, you would need to cast an instance to the internal interface, which would be possible only within the assembly it is defined.

    internal interface IClassName { string Name { get; set; } }
    
    public class MyList : Collection<IClass>
    {
        protected override void InsertItem(int index, IClass item)
        {
            var className = item as IClassName;
            if (className != null)
            {
                // do something with className.Name ...
            }
            base.InsertItem(index, item);
        }
    }
    
    public class B : IClassName { string IClassName.Name { get; set; } }
    

    Note I’m not using the new keyword to bypass the base class’ Add method since this would override the polymorphism (e.g. if you’d cast an instance of MyList to IList<IClass> the original implementation of Add would be called – and that’s bad.) Instead I’m overriding the appropriate method from the Collection<T> class, which is designed for collection inheritance.

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

Sidebar

Related Questions

Question Is there a way to define a method only once in C# (in
Is there a way to assign or define group within html markup?
Is there a way, using MySQL 5.0, to define a column in a table
Is there a way to define an enum in AS3 in a way we
Is there any way to define a variable that can be used in multiple
Is there a concise way to define properties in a ViewModel for data binding
Hi All is there any way to locally define a variable in a function
Is there a way that I can define __init__ so keywords defined in **kwargs
Is there a way to get the C++ pre-processor to expand a #define'ed value
I need to define an <img>'s src attribute in CSS. Is there a way

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.