开发者

Clearest way to declare a char value containing a single quote/apostrophe

开发者 https://www.devze.com 2023-03-01 12:28 出处:网络
To declare a char value in C# we just surround the character with single quotes: \'x\'. But what is the \"clearest\" way to declare a char value that is a single quote/apostrophe?

To declare a char value in C# we just surround the character with single quotes: 'x'.

But what is the "clearest" way to declare a char value that is a single quote/apostrophe?

I've ended up using "'"[0], though I had expected '''' to work (on the 开发者_运维百科basis that "" can be used to delimit a quote character within a string.

Is there a sensible, more succinct option?


You can escape the quote with a backslash: '\''


You can also use '\'' or (char)39


I think you're looking for '\''


I guess it's a matter of personal preference, I find escaping it clearest, eg:

char c = '\'';


You could always just try:

 char c = '\'';


For a char I would use

myChar = '\'';

The backslash is the standard escape key in both strings and characters, and most people should be able to understand this just fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消