19

I code in C# primarily these days, but I coded for years in VB.NET. In VB, I could combine a character constant and a string literal to create other constants, which is very handy:

Const FileExtensionSeparatorCharacter As Char = "."c
Const BillingFileTypeExtension As String = FileExtensionSeparatorCharacter & "BIL"

Now I'd like to do the same in C#:

const char FileExtensionSeparatorCharacter = '.';
const string BillingFileTypeExtension = FileExtensionSeparatorCharacter + "BIL";

but this give me a compiler error:

The expression being assigned to 'BillingFileTypeExtension' must be constant

Is there a reason I can't do this in C#?

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • 1
    `FileExtensionSeparatorCharacter` is an Awful Variable Name. Why not `FileExtensionSeparator`, or perhaps best `FileExtSep`? – cat May 17 '16 at 19:56
  • @cat -- Haha you're joking, right? – rory.ap May 17 '16 at 19:57
  • I don't actually care much, but I don't know why you'd wanna type that every time when you could save your fingers without sacrificing meaning. ¯\_(ツ)_/¯ – cat May 17 '16 at 20:00
  • 6
    Not sure if my experience with Visual Studio is typical, but I only type out full variable names during declaration. Every other time I just mash the keyboard and the variable I'm thinking of magically autocompletes. – Harrison Paine May 17 '16 at 20:17
  • 3
    Nice downvote Mr. Mystery Guest. Although the cause of the problem is a simple developer error, this is still a legitimate question. If a developer is used to VB.NET, which is a language that is usually more "laid back", problems like this might occur. – Visual Vincent May 17 '16 at 20:25
  • @cat I'm a strong believer in verbose identifier names. Why not? <-- it sounds like such a simple question, but dig deep within yourself, go on a philosophical journey, ask yourself "why not use long names?". I think you'll ultimately discover that you can't come up with a good reason. `FileExtSep` on the other hand...now *that* is a truly awful variable name. That's why I seriously thought you were joking. – rory.ap May 17 '16 at 20:25
  • @VisualVincent -- Thanks for the support. I'm scratching my head too on the down vote. – rory.ap May 17 '16 at 20:27
  • No problem. I hate when people don't explain their downvotes (where genuinely bad questions are an exception). It's probably someone who's got nothing better to do :). – Visual Vincent May 17 '16 at 20:31
  • 1
    I think people often confuse the purpose for down voting. Questions that people consider "dumb" -- e.g. the person is very inexperienced and asks a question that has an answer which is obvious to more-experienced people -- often get down voted when that's not what should happen. In this case, I'll bet the down voter read the question, didn't know the answer right away, read the answers and was like "oh yeah...duh, that makes sense...what a dumb question" and went back and down voted. – rory.ap May 17 '16 at 20:34
  • Your last part was an exact description of how I experienced this downvoter too! – Visual Vincent May 17 '16 at 20:40
  • 1
    @VisualVincent -- Another possibility is that C# lovers *hate* when you suggest that there is a feature in VB.NET that is superior to C#. Check out the up/down vote total on my question here. Utterly flabbergasting: http://stackoverflow.com/questions/29280864/what-makes-it-so-that-not-every-event-is-available-in-the-designer-and-how-can – rory.ap May 17 '16 at 20:42
  • Woah, and that question was even more legitimate than this! Then this only tells us one thing: Due to that VB.NET is so "much" simpler than C#, VB.NET developers are much nicer and calmer than the C# devs. The annoying syntax of C# makes its devs grumpy. :) _-- I'm probably about to start a war here, but yeah..._ – Visual Vincent May 17 '16 at 20:57
  • 2
    @cat because there is zero ambiguity in the first former. Its very hard to confuse verbose identifiers as it reads easily since we are English readers first code readers second. FileExtSep is the second worst only beaten out by 'FES'. – codemonkeyliketab May 17 '16 at 21:02
  • 1
    @VisualVincent -- I don't know if I'd agree with your points necessarily on VB.NET vs. C#. I love C#, and much better than VB.NET. I came from a java background before either, so I'm familiar with both "styles". What I have a problem with is people who disparage VB.NET for no good reason and refuse to accept that there are pros and cons to both languages. VB haters have a hard time realizing that it's just better than C# in some respects. I could make a list 3 pages long. But then so, too, is C# better than VB in many, many ways. – rory.ap May 17 '16 at 21:11
  • My previous comment was a joke though, I wasn't serious on any of the points I wrote :). I like C# rather much too, even if I'm pretty much only a Visual Basic developer. But I'd like to work more in C# if I had had the time and will. – Visual Vincent May 17 '16 at 21:13

2 Answers2

51

Is there a reason I can't do this in C#?

Yes, but you're not going to like it. The string concatenation involved in char + string involves implicitly calling ToString() on the char. That's not one of the things you can do in a constant expression.

If you make them both strings, that's fine:

const string FileExtensionSeparator = ".";
const string BillingFileTypeExtension = FileExtensionSeparator + "BIL";

Now that's string + string concatenation, which is fine to occur in a constant expression.

The alternative would be to just use a static readonly field instead:

const char FileExtensionSeparatorCharacter = '.';
static readonly string BillingFileTypeExtension = FileExtensionSeparatorCharacter + "BIL";
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Nice, was wondering exactly why it was that it wasn't compile-time, the implicit ToString makes sense. – Jonathon Chase May 17 '16 at 19:11
  • I see that now, but still...why is VB okay with compile time `ToString`? – rory.ap May 17 '16 at 19:12
  • @AdamV -- VB doesn't use single quotes for char literals. The literal with double quotes followed by a `c` is how you denote char literals. – rory.ap May 17 '16 at 19:13
  • 4
    @roryap: It has different rules, basically. I'm not familiar with the VB spec, but the C# 5 spec lists the operations in constant expressions in section 7.19. – Jon Skeet May 17 '16 at 19:13
19

I have to assume here that adding a character to a string is not considered a compile time constant, but rather a a run-time operation. If you change the type of FileExtensionSeparatorCharacter to string you will compile just fine.

const string FileExtensionSeparatorCharacter = ".";
const string BillingFileTypeExtension = FileExtensionSeparatorCharacter + "BIL";
Jonathon Chase
  • 9,396
  • 21
  • 39