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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:11:58+00:00 2026-05-26T04:11:58+00:00

I would like to bring access to an internal array with a Property, but

  • 0

I would like to bring access to an internal array with a Property, but controlling the access to array elements.

I have write a simple example that can explain my problem better than myself.

In the example, I provide a ‘Fail’ class and a ‘Controlled’ class. The second one runs as I would like, but the approach is a bit different and it is usefull only with one array.

My question is the next:
What about if I must to have two different arrays and therefore two differenct properties.

How to do it ?

Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The Fail Class:");

            MyFailClass MyFailTestClass = new MyFailClass(5);
            MyFailTestClass.MyList[2] = 11;
            if (MyFailTestClass.Modified) {
                Console.WriteLine("Right");
            } else {
                Console.WriteLine("Error");
            }



            Console.WriteLine("The Controlled Class:");

            MyControlledClass MyControlledTestClass = new MyControlledClass(5);
            MyControlledTestClass[2] = 11;
            if (MyControlledTestClass.Modified) {
                Console.WriteLine("Right");
            } else {
                Console.WriteLine("Error");
            }

            Console.ReadKey();
        }
    }

    public class MyFailClass
    {
        // Property 
        public byte[] MyList
        {
            get
            {
                return myList;
            }
            set  // <--------- Never enters here if I set a concrete array element
            {
                Modified = !myList.Equals(value);
                myList = value;
            }
        }
        public bool Modified { get; set; }

        // Constructor
        public MyFailClass(int elements)
        {
            myList = new byte[elements];
        }

        private byte[] myList;
    }

    public class MyControlledClass
    {
        // Property 
        public byte this[int index]
        {
            get
            {
                return myList[index];
            }
            set
            {
                Modified = !myList[index].Equals(value);
                myList[index] = value;
            }
        }
        public bool Modified { get; set; }

        // Constructor
        public MyControlledClass(int elements)
        {
            myList = new byte[elements];
        }

        private byte[] myList;
    }
}
  • 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-26T04:11:59+00:00Added an answer on May 26, 2026 at 4:11 am

    If you want two arrays, it stands to reason that you might later want three, or four (I don’t know for sure, but that seems to be how things go).

    In this case, I would consider making a class that’s an aggregate of your “MyControlledClass”

    public class IndexedClass
    {
        // Property 
        public byte this[int index]
        {
            get
            {
                return myList[index];
            }
            set
            {
                Modified = !myList[index].Equals(value);
                myList[index] = value;
            }
        }
    
    }
    
    public class IndexedClassGroup
    {
        // Property 
        public IndexedClass this[int index]
        {
            get
            {
                return myList[index];
            }
            set
            {
                Modified = !myList[index].Equals(value);
                myList[index] = value;
            }
        }
    
    }
    

    Then, you could access these things like a two dimensional array.

    Personally, I’m a little leery of exposing an array as a gettable/settable concept, in theory, so I don’t know a whole lot about the ins and outs of doing that. Whether a classes uses an array or a list or whatever seems like a private implementation detail rather than a public property. If you’re going to expose something, expose an ICollection<> or IEnumerable<> and resolve it internally to an array. My two cents, anyway.

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

Sidebar

Related Questions

I have 8 cells that are being built in my UITableViewController. I would like
I would like to bring up the Camera in my app, and let the
I need to fire of a bunch of threads and would like to bring
I would like to prevent a property from being exposed via my WCF web
I would like to monitor a log file that is being written to by
I would like to contribute to an open-source Windows Mobile project, but it's being
I have this method, that will be called against from a WCF Client, but
Suppose I have a custom collection class that provides some internal thread synchronization. For
In C# I have an intrusive tree structure that looks like this: public abstract
I'd like to have real-time access to both interpreter input and error and standard

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.