tags:

views:

282

answers:

3

That's pretty much the whole question. I've installed Visual C# 2008 Express on a clean Windows XP Pro SP3 machine, started a new Windows Forms project, double-clicked the form to create a Form1_Load method, then typed:

StringBuilder SB;
SB = new StringBuilder("test");
SB.Chars

but Chars doesn't appear on the autocomplete menu. Why not?

+4  A: 

It's the indexer, which you reference with foo[bar] syntax instead of foo.Chars(bar):

StringBuilder sb = new StringBuilder("Hello");
char c = sb[1]; // c='e'

C# doesn't use the names of indexers, nor can it use multiple indexers with the same parameters which are named differently (created in a different language).

Most of the time this is okay, but just occasionally I wish it supported named indexers fully...

Jon Skeet
+2  A: 

It's because you're using C#.

In C#, properties that take parameters can't be referred to by name, while in VB they can be used by name. You can access this property in the same way as an Items property off of a collection:

StringBuilder sb = new StringBuilder();
char ch = sb[0];

This will return the first character in the StringBuilder's internal string.

Andy
A: 

Thanks, both, for the quick and correct answer.

matthewk