Is it possible to write a property for a 2D array that returns a specific element of an array ? I’m pretty sure I’m not looking for an indexer because they array belongs to a static class.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
It sounds like you want a property with parameters – which is basically what an indexer is. However, you can’t write static indexers in C#.
Of course you could just write a property which returns the array – but I assume you don’t want to do that for reasons of encapsulation.
Another alternative would be to write
GetFoo(int x, int y)andSetFoo(int x, int y, int value)methods.Yet another alternative would be to write a wrapper type around the array and return that as a property. The wrapper type could have an indexer – maybe just a readonly one, for example:
Then: