Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 308
Shimmy Weitzhandler Avatar asked Jan 17 '23 09:01

Shimmy Weitzhandler


2 Answers

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.

like image 142
dlev Avatar answered Jan 31 '23 18:01

dlev


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?

like image 34
userx Avatar answered Jan 31 '23 19:01

userx