简体   繁体   中英

How are strings VS chars handled in C# vs Javascript?

In JavaScript, single and double quotes are somewhat interchangeable and largely a matter of styles (There is a good discussion of why this isn't actually the case in one of the answers here: When to use double or single quotes in JavaScript? ). How are chars and strings handled in C#?

For example:

string test = "hello world";
string test2 = 'hello world'; // Too many characters in character literal
char test3 = 'a';
char test4 = "a"; // Cannot implicitly convert type string to char

It looks like strings and chars are being handled as separate, interchangeable types, and that the use of single or double quotes demarcates this?

What is the relationship between chars and strings in typed languages? Specifically, would it be correct to say that a string is an array of chars?

would it be correct to say that a string is an array of chars

In .NET, a string is an object containing a contiguous block of memory containing UTF-16 code units. A char is another (primitive) data type that just contains one code point, with no object overhead.

From this interesting blog post from Jon Skeet , where he compares the .NET vs. Java implementation:

A long string consists of a single large object in memory. Compare this with Java, where a String is a “normal” type in terms of memory consumption, containing an offset and length into a char array – so a long string consists of a small object referring to a large char array.

C# uses the quotes to indicate the type - ' is always a char , " is always a string .

In .NET a string behaves like a read-only array of char , so:

// Treat string like an array
char c = "This is a string"[3];
int len = "This is a string".Length

// Now we have the char at pos 3
c == 's';

What you can't do is edit them:

// This fails
"This is a string"[3] = 'x';

// This is fine, because we get a new editable char[]
char[] c = "This is a string".ToCharArray();
c[3] = 'x';

This is true in any .NET language as they all use the same string implementation. Other strongly typed frameworks have different ways of handling strings.

In .NET char can be explicitly cast to an int and implicitly cast back, so:

char c = (char) 115; // 's'
int i = c; // 115

Finally char is a value type, while string (being a collection of character bytes under the covers) is actually an immutable reference type. These behave very similarly in code but are stored in different ways in memory - I think this is why C# makes the distinction in how they are delimited - "s" and 's' are different things and stored in different ways (entirely unlike the Javascript string ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM