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.
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#.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With