C Sharp char: Difference between revisions
From Coderwiki
More actions
new: info, size, example |
add: casting |
||
| Line 16: | Line 16: | ||
string c = "hello world"; | string c = "hello world"; | ||
char d = c[4]; // 'o' | char d = c[4]; // 'o' | ||
</syntaxhighlight> | |||
== Casting == | |||
To [[Casting|cast]] to a char, you can use the [[C Sharp casting|casting operator]] or the [[C Sharp Convert.ToChar|Convert.ToChar]] method:<syntaxhighlight lang="cs" line="1"> | |||
sbyte a = 65; | |||
char b = (char)a; // 'A' | |||
char c = Convert.ToChar(a); // 'A' | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 13:05, 19 August 2025
The char data type in C Sharp is used to store a single character value, usually in a variable.
It is the base unit for the string data type (see C Sharp string).
See Char for general information about the uses and memory layout of a character data type.
Size
edit edit sourceValues of type char take up 2 bytes in memory in C#.
This means they can store 65536 different character values.
Example
edit edit sourcechar a = 'a';
char b = '&';
string c = "hello world";
char d = c[4]; // 'o'
Casting
edit edit sourceTo cast to a char, you can use the casting operator or the Convert.ToChar method:
sbyte a = 65;
char b = (char)a; // 'A'
char c = Convert.ToChar(a); // 'A'