Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port pascal const IdentChars = ['a'..'z', 'A'..'Z', '_']; declaration to C#

Tags:

c#

delphi

I am porting a Delphi application to C#. In one of the units there is a declaration like this:

const
  IdentChars = ['a'..'z', 'A'..'Z', '_'];

I did not found similar declaration syntax for C#.

This is the best I could come up with:

char[] identFirstChars; // = ['a'..'z', 'A'..'Z', '_'];
int size = (int)'z' - (int)'a' + 1 + (int)'Z' - (int)'A' + 1 + 1; 
identFirstChars = new char[size];
int index = 0;
for(char ch = 'a'; ch <= 'z'; ch = (char)((int)(ch) + 1))
{
    identFirstChars[index] = ch;
    index++;
}
for (char ch = 'A'; ch <= 'Z'; ch = (char)((int)(ch) + 1))
{
    identFirstChars[index] = ch;
    index++;
}
identFirstChars[index] = '_';

There must be a more efficient way.

like image 466
RM. Avatar asked Jun 12 '11 06:06

RM.


2 Answers

IdentChars is a set, which has no direct equivalence in C# (a bit of a pain really). Secondly, IdentChars is a set of Ansi characters, not Unicode characters, so just be careful there. So, best to look at how it is used before "porting", because the functionality that you require is built into the Deplhi compiler and you will have to code this yourself in C#.

like image 71
Misha Avatar answered Sep 24 '22 17:09

Misha


What about this?

char[] identFirstChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_".ToCharArray();

Of course, you can generate an array in your code (this probably can be done with much less lines using Enumerable.Range) but I think in your case it doesn't worth it.

like image 34
Yuri Stuken Avatar answered Sep 22 '22 17:09

Yuri Stuken