Question: Why C# doesn't allow passing parameter as final? #1126
Replies: 62 comments
-
That's coming in C# 7.2, using See SharpLab example for the feature in action. (Or download a preview of VS2017.5 to try it out). |
Beta Was this translation helpful? Give feedback.
-
Nope. |
Beta Was this translation helpful? Give feedback.
-
How is it different in practice? |
Beta Was this translation helpful? Give feedback.
-
Hmm, that should be obvious.
I would hope that people don't start using |
Beta Was this translation helpful? Give feedback.
-
Also, take into account that if you're passing a struct type (like |
Beta Was this translation helpful? Give feedback.
-
Ah, that's unfortunate as that is exactly what I plan on doing. I don't really care what the compiler does under the hood. |
Beta Was this translation helpful? Give feedback.
-
I hope I'll never have to read/update code written by you 😁 |
Beta Was this translation helpful? Give feedback.
-
Just to clarify, your position is:
Questions:
|
Beta Was this translation helpful? Give feedback.
-
@DavidArno I can't think of many reasons not to add a Regarding Question 3, waiting would be a problem because I'm sorry this turned into another rant... |
Beta Was this translation helpful? Give feedback.
-
But |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr indeed, I meant only to refer to the syntax, to suggest that people might ask "why can't I type |
Beta Was this translation helpful? Give feedback.
-
Some points about performance (which I agree is pretty irrelevant; but, as I understand it An Really, any external call could modify an I vaguely recall reading that when passing an rvalue to Again, I really only care about the semantic concerns which throw up these same performance concerns, and I doubt that legitimate use of |
Beta Was this translation helpful? Give feedback.
-
Well, I don't know, the simple fact that |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Doesn't the fact that
|
Beta Was this translation helpful? Give feedback.
-
For small value types its bad; unless you want the live link behviour (which is more-likely an unexpected side-effect)
For large value types its good
tl;dr For large value types it skips this bit |
Beta Was this translation helpful? Give feedback.
-
@benaadams anyhow, why not then use it automatically when you can profit from it? It seems that the rule is simply |
Beta Was this translation helpful? Give feedback.
-
Because they are different things public class Program
{
private static int _value;
private static void AddToValue()
{
_value += 5;
}
private static void PrintValueByParam(int value)
{
Console.WriteLine(nameof(PrintValueByParam));
Console.WriteLine(value);
AddToValue();
Console.WriteLine(value);
}
private static void PrintValueByIn(in int value)
{
Console.WriteLine(nameof(PrintValueByIn));
Console.WriteLine(value);
AddToValue();
Console.WriteLine(value);
}
public static void Main(string[] args)
{
_value = 0;
PrintValueByParam(_value);
_value = 0;
PrintValueByIn(in _value);
}
} Outputs
|
Beta Was this translation helpful? Give feedback.
-
@benaadams so this is a low-level unsafe feature for bit hacking. Am I right? |
Beta Was this translation helpful? Give feedback.
-
It's a "lower-level" safe feature for fast math. Operations like this are common in libraries like XNA and they are currently burdensome to use in C# since to be performant the parameters are declared I would agree that it is somewhat niche compared to the C# market as a whole and does require a good degree of understanding on the part of the library authors in order to use correctly. |
Beta Was this translation helpful? Give feedback.
-
Not really its to both imply intent and have compiler enforcement of it e.g. previously you'd be using
However So its so you can do:
Or the shorter
Which implies the intent better and also enforces only read in the method. Probably what many people are after is: "readonly for locals and parameters" #188 so you could do:
i.e. "readonly by value" rather than "readonly by reference" |
Beta Was this translation helpful? Give feedback.
-
How exactly does |
Beta Was this translation helpful? Give feedback.
-
I assume
|
Beta Was this translation helpful? Give feedback.
-
The question I suppose is would |
Beta Was this translation helpful? Give feedback.
-
IMO it would be even more confusing. Developer X:
|
Beta Was this translation helpful? Give feedback.
-
It's a quartet. We already have "no modifier" which already means "read only by called function" by virtue of being by value. The new modifier means "read only reference by called function", which
I disagree. That's really my only concern here. Combined with the fact that this feature is relatively niche and requires a decent working knowledge of structs I don't think that the feature should be made super-easy to (mis)use. I'm fine with the omission of the modifier from the call-site though. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I read @benaadams 's comment regarding Addendum: I agree that having |
Beta Was this translation helpful? Give feedback.
-
I'm not sure how much sense that makes given that you can pass expressions to the |
Beta Was this translation helpful? Give feedback.
-
I mean to the caller, these are identical: void PrintValue(int value)
void PrintValue(readonly int value) So the |
Beta Was this translation helpful? Give feedback.
-
@HaloFour You are right that it would look odd passing an expression to a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In Java, you can mark variable as final
Why this is not allowed in C#.
There are many questions like this in Stackoverflow but I prefer to ask here to get the right reason behind that, not just theoretical answers.
Beta Was this translation helpful? Give feedback.
All reactions