I would like to ask if there is a more elegant way to do this:
List<char> unallowed = new List<char>();
for (char c = '\u0000'; c <= '\u0008'; c++) {
unallowed.Add(c);
}
for (char c = '\u000B'; c <= '\u000C'; c++) {
unallowed.Add(c);
}
// And so on...
I have to add to the list a few contiguous ranges of Unicode characters and the only thing that I can think of to refactor the code above is to create my own method to avoid typing the for cycles repeatedly. And I’m not even too sure it’s worth it.
Well, you could do something like:
Not sure it is worth it, though – especially given the need to use “count” rather than “end”. Probably easier to write your own extension method…
Re your comment; extension methods aren’t a .NET 3.5 feature. They are a C# 3.0 feature. So as long as you compile the code set to target .NET 2.0 / 3.0 (as appropriate), it doesn’t matter if the client doesn’t have .NET 3.5; you do, however, need to defined the
ExtensionAttribute– a few lines of code only:Or just go for broke and download LINQBridge and use all of LINQ-to-Objects in .NET 2.0.