Click here to Skip to main content
11,928,754 members (54,030 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked

Using a Property to store an Array

, 19 May 2005
Rate this:
Please Sign up or sign in to vote.
How to use a C# property to store and retrieve an array.


I found I had a need to store simple arrays in properties for easy retrieval throughout an application such that they may be set in form A, displayed in form B or report A, further changed by form C and then viewed again in form A or form B or report A or report B. This is not the only solution to this problem (indexers, for example) but it suited me and, what I think makes it interesting, is that I could not find any documentation describing this method of using a property and an array anywhere.

Using the code

The code can be wrapped into a class similar to:

using System;

namespace MyNamespace {
    /// <summary>
    /// Class to contain application properties.
    /// </summary>
    public class MyClass {
        public MyClass() {}

        /// <summary>
        /// Size of array.
        /// </summary>
        public const int Counter = 10;

        /// <summary>
        /// Widget: an array of widgets.
        /// </summary>
        private static int[] _widget = new int[Counter];
        public static int [] Widget {
            get { return _widget; }
            set { _widget = value; }

The property can be populated as follows...

for (int <code>i</CODE>

... and retrieve and use the array as follows:

double <code>_newWidget3</CODE>

Points of interest

The property handles all of the indexing with no further intervention such that any array item inserted at point x can always be retrieved by referring to point x in the call. It also appears comfortable with a variety of data types such as int, string, object. Though I haven't tried every type, I see no reason for it not to work with other data types.

Widget properties are defined as static types. This is key to leveraging the power of using a property array across a variety of different objects (forms, reports, etc.) having differing scopes as well as having the ability to alter the values throughout the lifetime of the application with minimal effort.

I am amazed that I wasn't able to find examples of this since it is so simple. If you have seen other examples, please let me know. I spent some time researching this, however, that doesn't mean I didn't miss something (as we all do) so please set me straight (politely!).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralTechnically works, but is very ineffecient Pin
rvhughes24-Oct-07 9:46
memberrvhughes24-Oct-07 9:46 
GeneralRe: Technically works, but is very ineffecient Pin
veki-peki11-Jul-08 0:04
memberveki-peki11-Jul-08 0:04 
QuestionProperty as an array ... bounds check? Pin
GrahamLuks18-Oct-07 3:34
memberGrahamLuks18-Oct-07 3:34 
QuestionSetting a multi-dimensional array property??? Pin
knoljo12-Dec-06 14:10
memberknoljo12-Dec-06 14:10 
GeneralInteresting Pin
Marc Clifton19-May-05 9:52
protectorMarc Clifton19-May-05 9:52 
GeneralRe: Interesting Pin
legalAlien19-May-05 22:31
memberlegalAlien19-May-05 22:31 
GeneralFxCop Pin
PaleyX19-May-05 8:05
memberPaleyX19-May-05 8:05 
GeneralRe: FxCop Pin
Ray Cassick19-May-05 15:43
memberRay Cassick19-May-05 15:43 
GeneralRe: FxCop Pin
Anonymous20-May-05 20:26
sussAnonymous20-May-05 20:26 
GeneralRe: FxCop Pin
Anonymous21-May-05 4:08
sussAnonymous21-May-05 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 19 May 2005
Article Copyright 2005 by legalAlien
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid