Adrian Serafin
Adrian Serafin

Reputation: 7725

c#, Textbox.Text encoding

I have code:

string text = sampleTextBox.Text;

and I'm wondering in what encoding text is? Is it utf16 (as it is string) or maybe it is my operating system encoding?

Upvotes: 3

Views: 5316

Answers (2)

Mikael Svenson
Mikael Svenson

Reputation: 39695

String variables in .Net are UTF-16 internally. Encoding comes into play when you want to output the string outside your program: a file, a webpage, or over the network in some fashion.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1504022

It's all Unicode, basically - there's no conversion between the .NET textual types (char/string) and binary going on, so there's no encoding to worry about.

You potentially need to worry about surrogate pairs to get from the UTF-16 textual representation of char and string to full UTF-32, but that's slightly different to the normal encoding issues.

Philosophically, a textbox contains text, not binary data. You should only be thinking about encodings when there's a conversion to a binary format - such as a file.

Upvotes: 10

Related Questions