Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size of char type in c#

Just wondering why do we have char type of 2 bytes size in C# (.NET) unlike 1 byte in other programming languages?

like image 213
Manish Basantani Avatar asked Jan 25 '10 17:01

Manish Basantani


People also ask

What is the size of char a 10 in C?

The size of char is 1 byte, and wikipedia says: sizeof is used to calculate the size of any datatype, measured in the number of bytes required to represent the type. However, i can store 11 bytes in unsigned char array[10] 0..

What is the size of int in C?

Most of the textbooks say integer variables occupy 2 bytes.


2 Answers

A char is unicode in C#, therefore the number of possible characters exceeds 255. So you'll need two bytes.

Extended ASCII for example has a 255-char set, and can therefore be stored in one single byte. That's also the whole purpose of the System.Text.Encoding namespace, as different systems can have different charsets, and char sizes. C# can therefore handle one/four/etc. char bytes, but Unicode UTF-16 is default.

like image 75
Jan Jongboom Avatar answered Sep 22 '22 01:09

Jan Jongboom


I'm guessing with “other programming languages” you mean C. C has actually two different char types: char and wchar_t. char may be one byte long, wchar_t not necessarily.

In C# (and .NET) for that matter, all character strings are encoded as Unicode in UTF-16. That's why a char in .NET represents a single UTF-16 code unit which may be a code point or half of a surrogate pair (not actually a character, then).

like image 20
Joey Avatar answered Sep 23 '22 01:09

Joey