4 ms·
I am having difficulty believing that someone wouldn't know how variables are scoped/refernced in function calls like the one you showed in the second example.
by abhishekjha 9y ago
I am having difficulty believing that someone wouldn't know how variables are scoped/refernced in function calls like the one you showed in the second example.
About the immutablity of strings : Is it absolutely necessary for a string to be immutable? What harm could it do otherwise? I learnt immutablity on my own and apprently there's no such constraint in languages like C/C++, although the same behaviour can be replicated using `const`.
I don't know C# but shouldn't `Console.WriteLine(s.Substring(1, 2));` have worked. No need to worry about immutability, at least in this case.
- LandR 9y agoStrings being immutable in .NET is a good thing, for a few reasons. e.g. string pooling (less assembly bloat), string interning (performance) and performance in general. You also get linq style benefits, e.g. string s = "this is a string"; if(s.Substring(5).ToUpperInvariant().StartsWith("I")) Console.WriteLine("true"); Console.WriteLine(s); If strings weren't immutable, you'd have to copy s to avoid changing / breaking it. These are things that I would expect a .NET developer to know. They are basic fundamentals.
- whyever 9y ago> performance in general. Really? Forcing you to copy whenever you want to modify gives you worse performance. > If strings weren't immutable, you'd have to copy s to avoid changing / breaking it. That seems like a disadvantage to me. With mutable strings, you have the option to modify it in place instead of being forced to copy.
- krzat 9y agoIdeally you have strings that can be both mutable and immutable based on context. C# is not that smart so they opted into the only other sane option.
- amarkov 9y agoThe advantage of making the string type immutable is that you can do what's known as "string interning": if the same string shows up multiple times in your program, only one copy has to be kept in memory. This is particularly useful for compile-time constant strings. C and C++ actually do a similar thing with their string literals, which are immutable even though normal strings aren't.
- brianmcc 9y ago`Console.WriteLine(s.Substring(1, 2));` _would_ work. You'd be printing the new string returned by the Substring() call. But `s.Substring(1, 2);` as a statement on its own does nothing - its new returned string is discarded on the wind.
- paganel 9y ago> I am having difficulty believing that someone wouldn't know how variables are scoped/refernced in function calls like the one you showed in the second example. I strongly believe it, in fact, just as I'm writing this I'm not sure what a language like PHP would print out in that example (maybe the latest versions do the "right" thing, but back in PHP3 and PHP4 times variable scoping was a mess).
- pjc50 9y agoInterestingly the two examples work together: the "x" is passed to foo by value, so a copy is modified and then not returned. If you have _mutable_ strings then write void Main() { string s = "foobar"; Foo(s); Console.WriteLine(s); } void Foo(string s) => s.Substring(1, 2); then this example behaves differently from the x one. Unless you copy strings on function call, which is potentially more expensive.
- jve 9y agoYou get the option to write C# code in an unsafe way, if you will (i.e modifying strings in place). Although i'v never needed to do such stuff when making line of business applications. https://www.codeproject.com/Articles/3377/Strings-UNDOCUMENTED https://www.codeproject.com/Articles/3377/Strings-UNDOCUMENT...