how do you right the 'get' and 'set' accessors for an array property

Posted on 2006-04-02
Last Modified: 2012-05-05
So I'm trying to use a instance variable that is an array. I want to write a property that will access this private instance variable. I can't find the syntax to do this.
Question by:john_schaffner
    LVL 18

    Accepted Solution

    Hi, syntax is:

          public class MyClass {
                //Instance array variable
                private string[] dataArray;

                public string[] Data {
                      get {
                            return this.dataArray;
                      set {
                            this.dataArray = value;
    LVL 18

    Expert Comment

    by:Ravi Singh
    If your defining a collection class then you could also make use of an indexer, which would allow users of your class to use the [] operator on a reference (just like using a standard array):


    MyClass myClass = new MyClass();
    string someData = myClass[0];



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now