C# should generalize the property and indexers to property index

W

wy6688

though C# have a indexers as class default index
property. in a class you can write

public class TestA
{
private byte[] A;
public byte this[int index]
{
get{ return A[index]}
set{ A[index]=value}
}

}.

but I think the indexers should be generalized to property
indexers. for example

public class TestB
{
private byte[] a;
private int [] b;

public byte A[int index]
{
get {return a[index])
set {a[index]=value)}
}

public byte B[int index]
{
get {return b[index])
set {b[index]=value)
}

}
 
J

Jeffrey Tan[MSFT]

Hi wy6688,

Based on my understanding, you have some concern on the design of C#
indexer.

=====================
Yes, you can only use the indexer in C# like this:
public class TestA
{
private byte[] A;
public byte this[int index]
{
get{ return A[index]}
set{ A[index]=value}
}
}.

This is by the design of C# language. It gives you the convinient to do
validation on the array.

For your suggestion design feature, I think you feel through that way you
can use your 2 internal arrays.

Actually, your design feature is not supported in C#. Because, C# is a
strong type language, while as you can see, in your design of:

public byte A[int index]
{
get {return a[index])
set {a[index]=value)}
}

public byte B[int index]
{
get {return b[index])
set {b[index]=value)
}

What does "A" and "B" mean? I think you can give any string to it. This is
not a good design, and the C# compiler can not recognize it.

Actually, in C#, you should what do you want like this:
public class TestB
{
public class A
{
private byte[] a;
public byte this[int index]
{
get
{return a[index];}
set
{a[index]=value;}
}
}

public class B
{
private byte[] b;
public byte this[int index]
{
get
{return b[index];}
set
{b[index]=value;}
}
}
}

Or you may provide 2 methods to access the 2 internal arrays.

===========================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi,effrey

Thank you for the suggestion

Though I could have other solution by the way your suggested
I still think C# should be improved to include the property array feature
Borland Dephi 7/8 include this feature, and provide default property array
as c# indexer. I think c# should be very easy to implement this feature
because it already implement the indexer

I'm porting some delphi code to C#, the code used a lots property array, an
I found I have to do lots of modification because this limitation of C#.

I do hope C# will include this feature in future version, and this feature won'
spoil the c# as a strong typed language

Thank

Wy668
 
J

Jeffrey Tan[MSFT]

Hi Wy6688,

Thanks very much for your feedback.

I have seen your concern. Actually, as I originally said, this is by the
design of C# language.

But I think you may provide your suggestion on this concern to Microsoft,
at below:
http://register.microsoft.com/mswish/suggestion.asp

or Email to: (e-mail address removed)

I think this may help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
L

Lord Crc

What does "A" and "B" mean? I think you can give any string to it. This is
not a good design, and the C# compiler can not recognize it.

And? If A and B were not indexed properties, your "argument" would
still hold.

This is one thing that i miss so much, i break the golden rule for it
(at least for my internal stuff), ie returning arrays.

There's no issue for the compiler to correctly identify and resolve

foo = myObj.A[ i ];
bar = myObj.B[ j ];

beyond what it already does for regular properties.

- Asbjørn
 
J

Jeffrey Tan[MSFT]

Hi Lord,

Sorry, I think I have a wrong idea on this design. Never mind :)

Yes, mark an indexer with "A" and "B" have nothing serious problem. Because
it is similiar as normal property definition.

Sorry for any confusion.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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