You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Standardize on a style for structs with no logic #249
We have a bunch of structs that are just a bundle of values with no logic - we are using private fields + properties in some places, and public readonly fields in other places.
Would be nice to use readonly fields everywhere we can.
If it doesn't hurt size/perf (which it shouldn't with inlining), C# conventions are to never expose public fields. What do you think of get-only properties (and sometimes primary constructors) from C# 6.0? The syntax looks like:
struct LocalDefinition (TypeDesc type, bool isPinned) // constructor parameters go next to the struct delcaration
{
public int SomeReadOnlyInt { get; } = Math.Max(3, 4); // Can do some logic here
public TypeDesc Type { get; } = type; // Can read constructor parameters here
public bool IsPinned { get; } = isPinned;
}
This convention is for public types in frameworks. It was invented so that the type implementation can change without breaking the framework public surface.
The type system is not designed to be public framework. It is our internal implementation detail. We do not need to follow the guidelines for public frameworks in the implementation. We should prefer concise tight code instead - that the guidelines for public frameworks are often in conflict with.
Activity
morganbr commentedon Nov 13, 2015
If it doesn't hurt size/perf (which it shouldn't with inlining), C# conventions are to never expose public fields. What do you think of get-only properties (and sometimes primary constructors) from C# 6.0? The syntax looks like:
MichalStrehovsky commentedon Nov 13, 2015
That C# 6 syntax is disgusting.
Although I do admit that in this case it has a nice side effect of making something that becomes de facto read only.
Not sure if we build with Roslyn on OSX and Linux though.
stephentoub commentedon Nov 13, 2015
FYI, the primary constructors feature was cut from C# 6
jkotas commentedon Nov 13, 2015
This convention is for public types in frameworks. It was invented so that the type implementation can change without breaking the framework public surface.
The type system is not designed to be public framework. It is our internal implementation detail. We do not need to follow the guidelines for public frameworks in the implementation. We should prefer concise tight code instead - that the guidelines for public frameworks are often in conflict with.
janvorli commentedon Nov 13, 2015
I'd also prefer public read-only fields.
Simplify interface dispatch map emission
Simplify interface dispatch map emission (#1253)