Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
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

Closed

Description

@MichalStrehovsky

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.

Activity

  1. morganbr commented on Nov 13, 2015

    @morganbr
    Contributor

    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;
    }
    
  2. MichalStrehovsky commented on Nov 13, 2015

    @MichalStrehovsky
    MemberAuthor

    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.

  3. stephentoub commented on Nov 13, 2015

    @stephentoub
    Member

    FYI, the primary constructors feature was cut from C# 6

  4. jkotas commented on Nov 13, 2015

    @jkotas
    Member

    C# conventions are to never expose public fields

    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.

  5. janvorli commented on Nov 13, 2015

    @janvorli
    Member

    I'd also prefer public read-only fields.

  6. added this to the milestone on Dec 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions