Why no array property in C#.

D

Ding Dang

I have been using Delphi for a few years, now using Delphi8 and VC#. I
just had a preview on VC# 2.0.

I was wondering why there is no array property? While there is in
VB.net, Delphi.net and IL.

For example, in Delphi, I could have

tStringsObject.strings;
tStringsObject.values['something'];
tstringsObject.keys;

Array property looks like an array, but does not necessarily need data
buffers.

Array property is really handy for writing/wiring components and
wrapping classes and data.

In C#, you could only have indexer which one for one class. Now when I
port some Delphi code or Delphi 8 codes to C#, I have to change the
interfaces to methods.

I had contacted the Microsoft C# team, and they said they had chose
not to support array property long time ago. "We chose simplicity over
flexibility in this case."


I understand that properties and array properties are essentially
member functions of a class. Published properties could appear on the
object inspector for you to define a collection of items of an array
property at design time.

Without array property, I have to then write codes to initialize items
of a collection. This is not good for RAD.

I heard from a 2-day dot Net seminar, Microsoft is not worried about
its dominant position on desktop applications, Microsoft is much
concerned by Linux on the server markets. Dot Net and C# mostly focus
on server, on which RAD development is not as appeal as on desktop.

I think this is one of the key factors why MS C# team did not want to
implement array property.



Best regards

Dingdang
 
J

James Curran

Ding said:
Array property looks like an array, but does not necessarily need data
buffers.
Without array property, I have to then write codes to initialize items
of a collection.

No you don't. Define you property as a instance of an internally
defined class. Define an indexer for that.

// Warning : untested code.
class StringsObject
{
private class SO_string
{
public string this[](int i) { ....}
}

public SO_string strings
{
get { .....}
}
}
 
A

Andrey

James said:
Ding said:
Array property looks like an array, but does not necessarily need data
buffers.

Without array property, I have to then write codes to initialize items
of a collection.


No you don't. Define you property as a instance of an internally
defined class. Define an indexer for that.

// Warning : untested code.
class StringsObject
{
private class SO_string
{
public string this[](int i) { ....}
}

public SO_string strings
{
get { .....}
}
}
Hm. that's interesting! Can you put a working piece of code? I just want to understand how exactly
to use it... I am still kinda newbie ;)


Thank you
Andrey
 
G

Guest

Hi,

Take a look at this Indexer tutorial
http://msdn.microsoft.com/library/d...ry/en-us/csref/html/vcwlkIndexersTutorial.asp

Regards,
Vikram, S

Andrey said:
James said:
Ding said:
Array property looks like an array, but does not necessarily need data
buffers.

Without array property, I have to then write codes to initialize items
of a collection.


No you don't. Define you property as a instance of an internally
defined class. Define an indexer for that.

// Warning : untested code.
class StringsObject
{
private class SO_string
{
public string this[](int i) { ....}
}

public SO_string strings
{
get { .....}
}
}
Hm. that's interesting! Can you put a working piece of code? I just want to understand how exactly
to use it... I am still kinda newbie ;)


Thank you
Andrey
 
B

Bob Grommes

James,

If the class is internally defined then how would a client know anything
about that internally defined type? The fact that it's exposed as a public
property would do you no good if you don't have a type definition to work
from.

What's wrong with:

class SO_string {
public string this[](int i) {...}
}

class StringsObject {
private SO_string so_string = new SO_string();

public SO_string strings {get {this.so_string;}}
}



--Bob
 
J

James Curran

Bob said:
If the class is internally defined then how would a client know
anything about that internally defined type? The fact that it's
exposed as a public property would do you no good if you don't have a
type definition to work from.

You're right (There are reasons why I label such things as "untested
code")
 
J

James Curran

OK, fully working code:
using System;
namespace PropertyArray
{
class StringsObject
{
public class SO_string
{
public string this[int i]
{
get
{
return i.ToString(); /// put something useful here.
}
}
}
private SO_string my_sos = new SO_string();
public SO_string strings
{
get {return my_sos; }
}
}
//-------------------------------------------------
class Class1
{
[STAThread]
static void Main(string[] args)
{
StringsObject so = new StringsObject();
Console.WriteLine(so.strings[456]);
}
}
}
 
J

Jon Skeet [C# MVP]

I heard from a 2-day dot Net seminar, Microsoft is not worried about
its dominant position on desktop applications, Microsoft is much
concerned by Linux on the server markets. Dot Net and C# mostly focus
on server, on which RAD development is not as appeal as on desktop.

I think this is one of the key factors why MS C# team did not want to
implement array property.

I don't think that's really a factor, to be honest. Properties with
parameters are just as useful in non-RAD development. This is one place
where I think Anders got it wrong, unfortunately. :(
 
R

Ravichandran J.V.

J

Jon Skeet [C# MVP]

Ravichandran J.V. said:
You could overload a class indexer. This way you could have more than
one class indexer in a class.

Yes, but it doesn't allow you to give them different names and access
them via the names, unfortunately.

For instance, suppose I have a class with some names in. It would be
nice to be able to do:

public class Foo
{
string[] names;

public string Names[int index]
{
get { return names[index]; }
}

// ...
}

rather than either exposing the names array itself (dangerous - you
can't keep it readonly) or creating a different class to expose the
values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top