-
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
-
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?
-
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
-
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.
-
Re: indexers and arrays
Thanks for your help
cheers
simon