Readonly parameters in primary contructor #7699
-
Hi, it will be very convenient if we can create readonly parameters in primary contructors, escpecially for injecting services. I don't want to variable (for example IService type) be mutable. Idea example: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
You and about 100,000 other devs want this feature. The last we heard from the team on this is documented in C# Language Design Meeting for July 31st, 2023:
Worryingly, in #7667, MS were still asking the question, "What else would you like to see and what are the important scenarios to you? ... Support for immutability in primary constructor parameters?" just last week. Whether that implies that either we haven't arrived at that "shortly" period of time yet, or the team are backtracking on ever doing this is unclear at the moment. |
Beta Was this translation helpful? Give feedback.
-
I'm sorry. I don't know how to was possible I didn't see this.
|
Beta Was this translation helpful? Give feedback.
-
The idea that came to my mind is that since the primary constructor has parameters similar to regular constructors or methods, it would be a good idea to add the ability to set "readonly" in parameters for both methods and constructors. Parameters are usually used only for reading data, and they are rarely changed, so it would be beneficial to introduce optional protection against accidental parameter overwriting. By the way, do I need to create a new thread for this extended idea to be noticed by someone from Microsoft's team? |
Beta Was this translation helpful? Give feedback.
You and about 100,000 other devs want this feature. The last we heard from the team on this is documented in C# Language Design Meeting for July 31st, 2023:
Worryingly, in #7667, MS were still asking the question, "What else would you like to see and what are the important scenarios to you? ... Support for immutability in primary constructor parameters?" just last week. Whether that implies that either we haven't arrived at that "shortly" period of time yet, or the team are backtracking on ever doing this is unclear at the moment.