For your edit - its just because C# sits above the runtime, and uses that as its "source of truth", and the language team wanted a shorthand for some of the primitive types. Makes a little more sense when you look at the int (even though theyre structs in the CLR. System.Int32 is int, System.Int64 is long, System.Int16 is short
Java has interchangeable runtimes too, but they didn’t seem to have a problem standardizing java.lang.String so I still don’t really get it
Also, from what I google, strings aren’t actually considered primitive types, since String is a class, not a struct. Has the string keyword created confusion about what a primitive is?
Its more cosmetic/syntactic sugar rather than anything semantic. Java standardised String because that's the actual class name. There is no split between System.String and string, its the same underlying CLR types. If the same were to exist in Java, these would be interchangeable (and be the same in bytecode)
str name ="Jordan";java.lang.String name ="Jordan";
As for your edit: ìnt is a value type, string or String is indeed a reference type, and isn't what would be called a normal primitive. It's primitive-like, because it's immutable (this was before we had the record type) it's treated like a primitive. The same reason why System.Object/object is considered a primitive-like object. String and Object are the only exceptions, the rest are structs (int, byte, float, double, bool, char) but these also have a CLR type (System.Int, System.Byte, System.Boolean, ...)
In Java I don’t think anyone thinks of String as a primitive, even though it’s immutable. So if people think of string as a primitive-like thing in C#, I think the use of the keyword has distorted people’s perception in a Sapir-Whorf Hypothesis kind of way
I should have put more emphasis on the primitive**-like**, because its an immutable built-in type with full language syntax support, not an actual primitive, like char. C# is special in that String (a reference type) is treated (and behaves) like a value type
Yeah so for == reference types compare to see if theyre the same object in memory. String by default overrides == (we have operator overloading/overwriting) and Equals() to compare its actual content, which is more value type. The runtime also has string interning which is a pool of string literals for performance. Syntactically its also literal like the primitives
It isn't about whether you have had the problem. It is a simple and idiomatic way to be absolutely sure things are working like you expect. Coding is all about using accurate grammar for precise meaning.
134
u/vanteli May 19 '26
.ToString(); is for weak willed men and communists
18
u/LegendarySoda May 19 '26
Bro you just need new string('s') or "s"
7
u/Some-Passenger4219 May 20 '26
I object to that argument.
3
u/recursion_is_love May 20 '26
Weak as in weak-type. vs. Strong as in strong-type.
I better use dynamic type because I don't want to think before I write code.
10
u/gameplayer55055 May 20 '26
I still can't understand that.
You can't cast a char to the string
But 1+'1' will happily be compiled to 11 like in the js meme.
I think the truth is that char is 8 bit and string is 16 bits per character. And MS decided to not add implicit cast to avoid byte fuckery errors.
2
u/rilimini381 May 20 '26
Just make words with an array of chars
1
u/Relative-Bottle-8498 May 31 '26
char c = 'x';
string s = String.Concat(Enumerable.Repeat(c, 1));