Clearest way to declare a char value containing a

2019-04-18 08:43发布

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?

标签: c# char
6条回答
甜甜的少女心
2楼-- · 2019-04-18 09:11

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

查看更多
Emotional °昔
3楼-- · 2019-04-18 09:17

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

查看更多
霸刀☆藐视天下
4楼-- · 2019-04-18 09:20

You could always just try:

 char c = '\'';
查看更多
Explosion°爆炸
5楼-- · 2019-04-18 09:22

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.

查看更多
迷人小祖宗
6楼-- · 2019-04-18 09:23

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

char c = '\'';
查看更多
放我归山
7楼-- · 2019-04-18 09:27

I think you're looking for '\''

查看更多
登录 后发表回答