CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2003
    Posts
    76

    indexers and arrays

    Hi

    I have a class which has several members which are arrays of doubles. I am trying to figure out the best way to access these arrays.

    Is it best to use properties:

    private static double[] _dcount = new double[20];

    public static double [] _dcount
    {
    get { return _dcount; }
    set { _dcount = value; }
    }

    or to write accessor functions ?

    I thought about using indexers, but if I am reading my books correctly they won't work if there are several arrays of the same type in the class.

    Any help would be appreciated

    cheers

    simon

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: indexers and arrays

    Properties are accessor methods, C# just gives you some nice syntactic sugar to make your life easier. So, use properties.

    However, do you really need to expose raw arrays like that? Why not maintain List<T> private fields and expose them as an IList<T> or a ReadOnlyCollection?

  3. #3
    Join Date
    Jan 2003
    Posts
    76

    Re: indexers and arrays

    Thanks BigEd

    I changed my code so that the arrays were no longer exposed. I'd be curious to see an example using IList

    cheers

    simon

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: indexers and arrays

    List<T> implements the IList interface, so you can treat it as an IList implicitly.

    Code:
    class Foo
    {
        private List<int> _someList;
    
        public IList<int> SomeList
        {
            get { return _someList; }
        }
    }
    exposing an interface is nice because someday you may change the List<T> to some other collection that also implements IList. If that happens none of the client code needs to change, you simply change the declaration of _someList.

  5. #5
    Join Date
    Jan 2003
    Posts
    76

    Resolved Re: indexers and arrays

    Thanks for your help

    cheers

    simon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured