Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104702

Is there a way to concat string with char as a const?

If I try

const char NoChar = (char)8470; //№
const char TmChar = (char)8482; //™
const string IdDisplayName = "Clements" + TmChar + ' ' + NoChar;

it will throw a compile error:

The expression being assigned to '{0}' must be constant

As far as I understand, this error occurs because when a char is because the string concatenation operator (+) internally calls ToString on the concatenated object.

My question is if there is a way (unmanaged?Tongue) to do it.

I need to pass that constant as an attribute and it should be generated on client.

The uglier workaround (will see what's uglier based on your answers...) is to subclass that attribute (which is sealed, will have to make some decompilation and copy-paste work) and embedding it as a non-const will be possible.

Upvotes: 4

Views: 1919

Answers (2)

userx
userx

Reputation: 3815

I assume that simply:

const string NoChar = "\x2116"; //№ - Unicode char 8470
const string TmChar = "\x2122"; //™ - Unicode char 8482
const string IdDisplayName = "Clements" + TmChar + " " + NoChar;

Is unacceptable?

Upvotes: 3

dlev
dlev

Reputation: 48596

You're allowed to specify unicode character values directly in a string via the \u escape. So const string IdDisplayName = "Clements\u2122 \u2116"; should get you what you want.

Upvotes: 6

Related Questions