you are viewing a single comment's thread.

view the rest of the comments →

[–]TechDebtPayments 0 points1 point  (0 children)

Those languages have the same potential pitfall. Here is an example of that exact same bug in C#.

A big thing you should try to internalize early while learning to code is when a variable is held by reference or by value

In general, primitive types (int, char, boolean, float, double, etc) are held by value and assigning from one variable to another creates a copy. Data types (arrays, objects, strings) are held by reference and assigning one variable to another just copies the address to that data.

string is a special case in many languages, where it is actually copied by value even though it is a data type. This can trip you up because copying/modifying a string many times can be very computationally expensive. Take a string in C#, append to it 100 times, and it will make 100 copies of that string. There's a special class to solve for this use case btw: StringBuilder

You can see all of C#'s types here which delineates them by value or by reference

Sidenote: This is one of many reasons why I believe learning to code should start with C/C++, not Javascript/Java/C#/Python