Port pascal const IdentChars = ['a' .. 'z', 'A' .. 'Z', '_']; ad in c #

I am porting a Delphi application in C #. One of the blocks has an ad like this:

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

I did not find a 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 should be a more efficient way.

+6
source share
6 answers

How about this?

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

Of course, you can create an array in your code (this can probably be done with much fewer lines using Enumerable.Range ), but I think in my case that it is not worth it.

+5
source

IdentChars is a collection that does not have direct equivalence in C # (actually this is a bit of a pain). Secondly, IdentChars is an Ansi character set, not Unicode characters, so be careful. Therefore, it is best to take a look at how it is used before β€œporting”, because the necessary functionality is built into the Deplhi compiler, and you will need to do this yourself in C #.

+6
source
 char[] ar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_".ToArray(); 
+1
source

Not the most efficient way, but at least you won't skip characters by mistake:

 var chars = Enumerable.Range('a', 'z' - 'a') .Concat(Enumerable.Range('A', 'Z' - 'A')) .Select(arg => (char)arg) .Concat(new[] { '_' }) .ToArray(); 
+1
source

try the following

  public static char[] GetConstants() { var array = Enumerable.Range((int) 'a', 26).ToList(); array.AddRange(Enumerable.Range((int) 'A', 26)); array.Add('_'); return array.Select(z => (char) z).ToArray(); } 
+1
source

char[] identFirstChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_".ToCharArray() is one quick and dirty way to do this;)

0
source

Source: https://habr.com/ru/post/890325/


All Articles