property for an array field

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

Classes should have private fields, and they should be accessed
through properties. But what if the field is an array? I know you
can use an indexer to use [] syntax on the entire class itself, but
what about using the [] syntax for a property that hides a field that
is an array?

Is this possible? I was pretty sure it was in VB, but I must be
mistaken, since I don't see it in C#. I am surprised if it is not
possible, since you'd have to code a method to make accessors for
array fields.

Zytan
 
Classes should have private fields, and they should be accessed
through properties. But what if the field is an array? I know you
can use an indexer to use [] syntax on the entire class itself, but
what about using the [] syntax for a property that hides a field that
is an array?

Is this possible? I was pretty sure it was in VB, but I must be
mistaken, since I don't see it in C#. I am surprised if it is not
possible, since you'd have to code a method to make accessors for
array fields.

No, C# doesn't have named indexers, which I think is what you mean.
Yes, it's a bit of a pain.

Jon
 
No, C# doesn't have named indexers, which I think is what you mean.
Yes, it's a bit of a pain.

Yes, that's just what I mean. Indeed it is a pain. Surprising that VB
has it and C# doesn't.

Thanks for the confirmation.

Zytan
 
Zytan said:
Yes, that's just what I mean. Indeed it is a pain. Surprising that VB
has it and C# doesn't.

You can fake it by creating or using a class which is itself indexable,
and exposing it as a property. E.g. see Collection<T>.

-- Barry
 
You can fake it by creating or using a class which is itself indexable,
and exposing it as a property. E.g. see Collection<T>.

Yes, that's true, thanks, Barry.

Zytan
 
Back
Top