✅ public readonly bool Equals(...)? - SOLVED
I created struct for my needs of representing a phone number prefix and intellisense offered me a "potential fix" which after applying added readonly modifier to a method return type? Can you explain to me what this does as I see it first time in my 5 years of c#-ing?
And yes... It compiles 😄
6 Replies
So the readonly modifier means that the method is restricted from modifying any data in your struct
nvm, I just found the documentation for it 😄 https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/readonly-instance-members
I wonder if it offers any performance benefits?
Readonly instance members - C# 8.0 draft feature specifications
This feature specification describes the syntax for declaring and using readonly instance members.
I highly doubt there are performance benefits
The method itself is simple enough to where even if you did gain performance benefits they'd most likely not be noticable
reading through the link I posted... it can save you some copies of the structs when using in parameters... so in case of equals method it wont do much, but in case of a bigger structures it can save memory
👍 you asked the question and I learned something new today
haha, same here, thank you 😄