Using thousands separator in the code - c#

My code requires bunch of large constant values. The definition would be much easier to read if thousands separators could be used in declarations. Is there any better way to declare the weight variables than conversion of a string (i.e. Convert.ToDouble("1,987,123.456"))?
Note: there are many questions/answers related output format, but no luck identifying anything for the C# code itself.

If you can use Visual Studio 2017, there's a new feature in c# 7.0 exactly for this:
C# 7.0 allows _ to occur as a digit separator inside number literals
So, you can use:
1_987_123.456;
But those are constants. Don't put them inside a string because they will be taken as part of it (not that there's any reason to hard-code a double inside a string, anyway).

Related

Any way to use string (without escaping manually) that contains double quotes

Let's say I want to assign a text (which contains many double quotes) into variable. However, the only way seems to manually escape:
string t = "Lorem \"Ipsum\" dummy......
//or//
string t = #"Lorem ""Ipsum"" dummy.....
Is there any way to avoid manual escaping, and instead use something universal (which I dont know in C#) keywoard/method to do that automatically? In PHP, it's untoldly simple, by just using single quote:
$t = 'Lorem "Ipsum" dummy .......
btw, please don't bomb me with critiques "Why do you need to use that" or etc. I need answer to the question what I ask.
I know this answer may not be satisfying, but C# sytnax simply won't allow you to do such thing (at the time of writing this answer).
I think the best solution is to use resources. Adding/removing and using strings from resources is super easy:
internal class Program
{
private static void Main(string[] args)
{
string myStringVariable = Strings.MyString;
Console.WriteLine(myStringVariable);
}
}
The Strings is the name of the resources file without the extension (resx):
MyString is the name of your string in the resources file:
I may be wrong, but I conjecture this is the simplest solution.
No. In C# syntax, the only way to define string literals is the use of the double quote " with optional modifiers # and/or $ in front. The single quote is the character literal delimiter, and cannot be used in the way PHP would allow - in any version, including the current 8.0.
Note that the PHP approach suffers from the need to escape ' as well, which is, especially in the English language, frequently used as the apostrophe.
To back that up, the EBNF of the string literal in current C# is still this:
regular_string_literal '"' { regular_string_literal_character } '"'
The only change in the compiler in version 8.0 was that now, the order of the prefix modifiers $ (interpolated) and # (verbatim) can be either #$ or $#; it used to matter annoyingly in earlier versions.
Alternatives:
Save it to a file and use File.ReadAllText for the assignment, or embed it as a managed ressource, then the compiler will provide a variable in the namespace of your choice with the verbatim text as its runtime value.
Or use single quotes (or any other special character of your choice), and go
var t = #"Text with 'many quotes' inside".Replace("'", #"""");
where the Replace part could be modeled as an extension to the String class for brevity.

How to programmatically get the list of symbols restricted in variable names

Just curios is there any way to get the list of symbols that are illegal for naming variables in C# e.g. brackets of all kinds, commas etc in code? Something like SomeClass.GetIllegalVariableSymbols()
No; there are a lot, for one, when you consider Unicode, and there are also other rules (e.g. an identifier can contain a number but not begin with one, keywords can’t be used as identifiers unless prefixed by an #, etc.).
You’ll need to build your own appropriate for the situation. (What is that, out of curiosity?)
Yes, you can. Just use Path.GetInvalidFileNameChars() method.
Source

Looking for simple yet powerful windows wildcards (`*, ?`) matching implementation

I'm looking for simple and powerful way to implement Windows flavoured * and ? wildcards matching in strings.
BeginsWith(), EndsWith() too simple to cover all cases, while translating wildcards expressions to regex'es will look to complex and I'm not sure about performance.
A happy medium wanted.
EDIT: I'm trying to parse .gitignore file and match the same files, as Git does. This means:
File should be out of repository's index (so I'm checking file's path against one stored in index)
Number of patterns in .gitignore can be large;
Number of files to check might also be large.
The equivalents of the Windows wildcards ? and * in regex are just . and .*.
[Edit] Given your new edit (stating that you're looking for actual files), I would skip the translation altogether and let .Net do the searching using Directory.GetFiles().
(note that, for some reason, passing a ? into Directory.GetFiles() matches "zero or one characters," whereas in Windows it always matches exactly one character)
To get an exact match including all corner-cases, use
System.IO.Directory.GetFiles(myPath, myPattern)
You may have to create some tempfiles form your targetstrings first.
In other words, I think you should keep your patterns dry until it's time to meet the filesytem.
You should go with regex based approach unless your data volume is humungous or you have data-points to say regex will severely impact performance.
If that is the case, any other solution will also likely affect the performance and you will probably need to hand-roll something.
Converting * and ? to regex is quite easy.
For ? replace the "?" with ".{1}"
and for * replace the "*" with ".+?"
That should get you the same behaviour as wildcard matching on windows.
EDIT:
boolean PathMatchSpec(input, pattern) will do the job.
Private Declare Auto Function PathMatchSpec Lib "shlwapi" (ByVal pszFileParam As String, ByVal pszSpec As String) As Boolean

How to detect a C++ identifier string?

E.g:
isValidCppIdentifier("_foo") // returns true
isValidCppIdentifier("9bar") // returns false
isValidCppIdentifier("var'") // returns false
I wrote some quick code but it fails:
my regex is "[a-zA-Z_$][a-zA-Z0-9_$]*"
and I simply do regex.IsMatch(inputString).
Thanks..
It should work with some added anchoring:
"^[a-zA-Z_][a-zA-Z0-9_]*$"
If you really need to support ludicrous identifiers using Unicode, feel free to read one of the various versions of the standard and add all the ranges into your regexp (for example, pages 713 and 714 of http://www-d0.fnal.gov/~dladams/cxx_standard.pdf)
Matti's answer will work to sanitize identifiers before inserting into C++ code, but won't handle C++ code as input very well. It will be annoying to separate things like L"wchar_t string", where L is not an identifier. And there's Unicode.
Clang, Apple's compiler which is built on a philosophy of modularity, provides a set of tokenizer functions. It looks like you would want clang_createTranslationUnitFromSourceFile and clang_tokenize.
I didn't check to see if it handles \Uxxxx or anything. Can't make any kind of gurarantees. Last time I used LLVM was five years ago and it wasn't the greatest experience… but not the worst either.
On the other hand, GCC certainly has it, although you have to figure out how to use cpp_lex_direct.

Heredoc strings in C#

Is there a heredoc notation for strings in C#, preferably one where I don't have to escape anything (including double quotes, which are a quirk in verbatim strings)?
As others have said, there isn't.
Personally I would avoid creating them in the first place though - I would use an embedded resource instead. They're pretty easy to work with, and if you have a utility method to load a named embedded resource from the calling assembly as a string (probably assuming UTF-8 encoding) it means that:
If your embedded document is something like SQL, XSLT, HTML etc you'll get syntax highlighting because it really will be a SQL (etc) file
You don't need to worry about any escaping
You don't need to worry about either indenting your document or making your C# code look ugly
You can use the file in a "normal" way if that's relevant (e.g. view it as an HTML page)
Your data is separated from your code
Well even though it doesn't support HEREDOC's, you can still do stuff like the following using Verbatim strings:
string miniTemplate = #"
Hello ""{0}"",
Your friend {1} sent you this message:
{2}
That's all!";
string populatedTemplate = String.Format(miniTemplate, "Fred", "Jack", "HelloWorld!");
System.Console.WriteLine(populatedTemplate);
Snagged from:
http://blog.luckyus.net/2009/02/03/heredoc-in-c-sharp/
No, there is no "HEREDOC" style string literal in C#.
C# has only two types of string literals:
Regular literal, with many escape sequences necessary
Verbatim literal, #-quoted: doublequotes need to be escaped by doubling
References
csharpindepth.com - General Articles - Strings
MSDN - C# Programmer's Reference - Strings
String literals are of type string and can be written in two forms, quoted and #-quoted.
November 2022 update:
Starting with C# 11 this is now possible using Raw string literals:
var longMessage = """
This is a long message.
Some "quoted text" here.
""";

Categories