question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get-only properties are emitted as readonly in CoreFx ref assemblies

See original GitHub issue

Get-only properties are generated as readonly properties when auto-generating ref assemblies. The autogeneration steps are as mentioned in updating-ref-source.md. This causes multiple changes in the ref assembly code; not sure if it is legit and should be accepted.

E.g.

-        public bool AllowTrailingCommas { get { throw null; } set { } }
-        public System.Text.Json.JsonCommentHandling CommentHandling { get { throw null; } set { } }
+        public bool AllowTrailingCommas { readonly get { throw null; } set { } }
+        public System.Text.Json.JsonCommentHandling CommentHandling { readonly get { throw null; } set { } }

And another one with subtle difference

-        public System.Buffers.ReadOnlySequence<byte> ValueSequence { get { throw null; } }
-        public System.ReadOnlySpan<byte> ValueSpan { get { throw null; } }
+        public readonly System.Buffers.ReadOnlySequence<byte> ValueSequence { get { throw null; } }
+        public readonly System.ReadOnlySpan<byte> ValueSpan { get { throw null; } }

Entire diff can be found here.

Creating this issue based on discussion in dotnet/corefx/issues/36125 in following comments, _@WinCPP in https://github.com/dotnet/corefx/issues/36125#issuecomment-500109703_ _@ahsonkhan in https://github.com/dotnet/corefx/issues/36125#issuecomment-500173705_

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
tannergoodingcommented, Jun 11, 2019

I will have to spend some understanding this feature a bit better, particularly potential drawbacks (https://github.com/dotnet/csharplang/blob/master/proposals/readonly-instance-members.md#drawbacks), so let me get back to you.

The drawbacks are just the same as not having the attribute, there are some cases which may still have hidden copies due to how the compiler handles things. This is more a concern about the implementation within a member marked readonly though, and in that you will get an compiler error if you try to invoke a non-readonly method (which just forces you to create an explicit copy).

Also, to summarize what @ahsonkhan asked me offline.

  • The attribute can exist for downlevel as well. Compilers that don’t understand the attribute will simply ignore it. So, something using C# 7.3 will consume the ref assembly that has the attribute and it will ignore it and potentially create hidden copies in applicable scenarios (the same as if the attribute didn’t exist at all). C# 8.0 and higher will consume the ref assembly and recognize the attribute, eliding the hidden copies in applicable scenarios.
0reactions
WinCPPcommented, Jun 12, 2019

Thanks @tannergooding for the info. Closing this issue as discussed above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is "readonly" meaningful on a getter-only ref struct property ...
So, yes, readonly is meaningful. I think the case where readonly is redundant only when the struct itself is already declared as readonly...
Read more >
How can I have read-only properties in my ...
Yes, it is possible to create read-only properties in Entity Framework (EF) classes. One way to achieve this is to use the private...
Read more >
Allow ref-returning get-only auto-properties #15623
The reality is that this feature should have been accompanied by readonly and some way to block the copying of user defined value...
Read more >
readonly vs. private getter-only property in C# 6
Both provide read-only access to a list that is initialised when an instance of the class is created. It's possible that it creates...
Read more >
Declare and Initialize Read-only Auto Properties in C# | ...
In the case of a field member, you get only one chance to initialize the field with a value, and that is when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found