r/programmingmemes 13d ago

Since when did they add ragebait to C#, because WHAT IS THIS

Post image
258 Upvotes

49 comments sorted by

134

u/vanteli 13d ago

char c = 'A'; string s = $"{c}";

.ToString(); is for weak willed men and communists

72

u/jordansrowles 13d ago

String interpolation calls ToString under the hood, cant call yourself strong willed if you add a middleman. Everyone knows real men are explicit

string s = new string(c, 1);

30

u/prehensilemullet 13d ago edited 13d ago

It boggles my mind that someone would decide to capitalize all member names but not all builtin class types 

Edit: apparently string is a builtin keyword alias for System.String.  I never felt the need for such shenanigans in Java

11

u/jordansrowles 12d ago

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

6

u/prehensilemullet 12d ago edited 12d ago

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?

5

u/jordansrowles 12d ago

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)

java str name = "Jordan"; java.lang.String name = "Jordan";

3

u/prehensilemullet 12d ago

In Java, classes in java.lang are always in scope.  Did they not do this for System in C#?  Maybe that’s the issue…

6

u/jordansrowles 12d ago

Before modern .NET we had to do

csharp using System;

If we wanted String (otherwise youd need to write System.String). But using the string keyword has never required us to bring System into scope.

Nowadays we have implicit usings, so pretty much most of the commonly used System.* namespaces are automatically included

2

u/prehensilemullet 12d ago

Yeah I bet if they had just automatically included it from the beginning of C# they would have never made a string keyword

3

u/jordansrowles 12d ago

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, ...)

1

u/prehensilemullet 12d ago

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

3

u/jordansrowles 12d ago

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

1

u/prehensilemullet 12d ago

Behaves like a value type in the sense of == comparing contents or what?

I see that C# supports operator overloading now though, so I guess that’s not unique to strings either

1

u/jordansrowles 10d ago

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

var name = "Jordan"; // no new String()

→ More replies (0)

1

u/dominjaniec 13d ago

well... anyone can define class String {}

3

u/prehensilemullet 13d ago

I had a zero problems with that in my Java days

2

u/AlwaysHopelesslyLost 12d ago

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.

1

u/moriturus_m 13d ago

except that bypasses internalization (iirc?)

1

u/SnooOpinions6959 10d ago

This probably also calls .to string() under the hood but I wanted to share it char c = 'c'; string s = "" + c;

12

u/Helen83FromVillage 13d ago

You can also do this:

  1. Write a C++ function taking a char and returning a string.
  2. Call it.

5

u/lomberd2 13d ago

You could also use bash or god forbid powershell

8

u/BobQuixote 13d ago

Python > PowerShell > Bash > Batch

5

u/lomberd2 13d ago

Python > PowerShell > Bash > Batch > 💩 ?

1

u/BobQuixote 13d ago

Batch: It's better than shit.

1

u/YTriom1 13d ago

Bash's superior to PowerShell imho

3

u/WildCard65 12d ago

I mean PowerShell gives you the full power of C# and dotNET runtime from the scripting side.

1

u/BlankMercer 12d ago

fish supremacy

1

u/duck1123 12d ago

Nushell > Python

2

u/Doug2825 11d ago

You can also pass a pointer to a lone char to a function that expects a pointer to a null terminated string

1

u/bookaddicta 12d ago

Dang, guess now I’m forced to use .ToString();

18

u/LegendarySoda 13d ago

Bro you just need new string('s') or "s"

8

u/Some-Passenger4219 13d ago

I object to that argument.

3

u/recursion_is_love 13d ago

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.

11

u/gameplayer55055 13d ago

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.

18

u/gameplayer55055 13d ago

UPD: I was wrong.

C# char is secretly ushort (16 bit too). BUT it is a value type while string is a reference type.

1+'1' will be compiled to 50, and 1+"1" is 11 because of some operator overload trickery.

3

u/prehensilemullet 13d ago

I was gonna say that would be a world of problems if a char was a byte

2

u/WildCard65 7d ago

Its actually more similar to Windows' wchar_t type which is for UTF-16 strings.

2

u/rilimini381 12d ago

Just make words with an array of chars

-2

u/Hot-Employ-3399 13d ago

This is called a common sense. Char is not a string. Fuck auto conversion. MLs got it right not allowing even float <-> int implicit casting 

1

u/Relative-Bottle-8498 1d ago

char c = 'x';
string s = String.Concat(Enumerable.Repeat(c, 1));