Does C# provide array properties (as in Delphi)?

  • Thread starter Thread starter Debbie Croft
  • Start date Start date
D

Debbie Croft

Does C# have the ability to define array properties, as in Delphi? For
example, does the following have an equivalent in C#?

type
TMyGrid = class

{ ...some code... }

property IntegerCells[i, j: Integer]: Integer read GetIntegerCell write
SetIntegerCell;
property StringCells[i, j: Integer]: Integer read GetStringCell write
SetStringCell;

{ ...some code... }

end;
 
No, you can only do this for indexers, but not for properties. This is a
real annoyance in C# (you can do it in VB.NET, so why not in C# is anybody's
guess).
 
No, you can only do this for indexers, but not for properties. This is a
real annoyance in C# (you can do it in VB.NET, so why not in C# is anybody's
guess).


Does C# have the ability to define array properties, as in Delphi? For
example, does the following have an equivalent in C#?
type
TMyGrid = class
{ ...some code... }
property IntegerCells[i, j: Integer]: Integer read GetIntegerCell write
SetIntegerCell;
property StringCells[i, j: Integer]: Integer read GetStringCell write
SetStringCell;
{ ...some code... }

Yes, but you can have a property that is a 2 dimensional array:

private int[,] _myArray;
public int[,] MyArray {
get { return _myArray; }
set { _myArray = value; }
}

Then initialize it and use it with this code:

this.MyArray = new int[10,10];
this.MyArray[5,5] = 25;

Chris
 
Chris Dunaway said:
Yes, but you can have a property that is a 2 dimensional array:
private int[,] _myArray;
public int[,] MyArray {
get { return _myArray; }
set { _myArray = value; }
}

Not very safe when one caller might completely replace the array that
another one wrote to!

Eq.
 
Exactly - it's not a proper substitute for having parameterised properties.
Why put something in the CLI then not bother to support it in a language
which was actually designed from the outset with the CLI in mind?

Paul E Collins said:
Chris Dunaway said:
Yes, but you can have a property that is a 2 dimensional array:
private int[,] _myArray;
public int[,] MyArray {
get { return _myArray; }
set { _myArray = value; }
}

Not very safe when one caller might completely replace the array that
another one wrote to!

Eq.
 
Exactly - it's not a proper substitute for having parameterised properties.
Why put something in the CLI then not bother to support it in a language
which was actually designed from the outset with the CLI in mind?

There are plenty of other CLI features which aren't supported by C#. I
agree that this is an annoying one though - maybe it'll be addressed
in C# 4.

Jon
 
Back
Top