It is basically: if we introduce it, it gives us a tiny benefit for a gigantic amount of noise during every single code review. It boils down to comparing developer productivity vs. language safety.
I find the argument a bit weak since code review guidelines can end this conversation AND C# has already quite some code analysis code fixes to auto-fix language usage stuff (like file scoped namespaces or nullability checks). And that case of const and readonly can be algorithmically checked.
But there are other groups at Microsoft which keep them on track with their real needs.
I suppose you can emulate it today with variable prefixes/suffixes and check the AST with a custom linter yourself
How it'd work in such a case:
You declare immutable variable "a" which's an instance of a class
and then you execute "DoSomething" method which mutates under the hood?
Compilation error? runtime error?
Anyway have you tried records?
Js has const, C has const, Java final.
Imho it'd be awesome if mutability were not the default and we would have to mark mutability, but until then any variable that doesn't need it, is final. Which also helps skimming code
C# has readonly and const
>Imho it'd be awesome if mutability were not the default
If they decided one day that - "let's make immutable by default", then it'd be performance hit - wouldn't it?
codeflo means to declare a local variable that cannot be reassigned later.
void Foo() {
// this is possible in C# today, but only for compile-time constants
const int Bar = 42;
// this is what codeflo wants: variables that cannot be changed after declaration, but can hold runtime values
readonly string text = $"Blah: {Bar + System.Environment.ProcessorCount}";
// with such a variable, you wouldn't be able to reassign it later:
text = "Some other text";
// the line above would be a compile-time error, just like trying to reassign to a readonly field.
}Not really. It's just a syntactic default, reassigning local variables should be discouraged from except for iterators etc. It just fosters bad habits.
For example, A list can still be mutated like usual, the reference however may not change. This usually allows compilers to do optimizations in multithreaded situations. But that's not why I code that way, it reduces the cognitive burden of keeping track which variables are actual moving parts as opposed to context/names etc.
> C# has readonly and const
Haven't written C# in a few years, GP was talking about variables as far as I can tell and their lack of immutability.
To your last question: Yes, records are really nice syntactic sugar for classes that want to behave like values.
And can be edited without unloading the project first.
Recently, I went back to C# for a smaller project. The .NET ecosystem certainly has its advantages compared to the “wild west” of NPM, which includes Visual Studio/JetBrains Rider.
My main problem was that the language feels so dated at this point. Why can’t I make local variables immutable? Why are there still no algebraic types? Why do the new nullability rules feel so inconsistent and tacked on, with “String?” and “DateTime?” behaving completely differently?
I realize not everybody cares about these “modern” features as much as I do, but once you’ve wired your brain to think about program composition in a way that simply feels better to you, it’s very frustrating to go “back”.
.
Edit: It’s not like the C# team is unaware of or in any way against these proposals. There just seems to be little visible progress:
Algebraic types: https://github.com/dotnet/csharplang/issues/113
Const locals: https://github.com/dotnet/csharplang/issues/188