Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. C# Compiler Support for ReadOnlyReferences

C# Compiler Support for ReadOnlyReferences

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharphelpannouncementhtmldotnet
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard DeemingR Richard Deeming

    If you're passing structs around as readonly references, you'll want to make sure the struct itself is readonly as well. The same applies if you're storing them in a readonly field. Otherwise, every time you access a property or method on the struct, the compiler will make a defensive copy to enforce the readonly-ness of the value. The ‘in’-modifier and the readonly structs in C# – Dissecting the code[^]


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    D Offline
    D Offline
    David A Gray
    wrote on last edited by
    #3

    That is precisely the point of the ReadOnlyReference modifier, which you would discover if you read that article all the way to the end.

    David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

    Richard DeemingR 1 Reply Last reply
    0
    • D David A Gray

      That is precisely the point of the ReadOnlyReference modifier, which you would discover if you read that article all the way to the end.

      David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #4

      Which one? The article from 2017, talking about the in modifier and readonly structs, which have already been released? Or the infoworld "subscriber only" article, which can't be read without signing up? :doh:


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      D 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Which one? The article from 2017, talking about the in modifier and readonly structs, which have already been released? Or the infoworld "subscriber only" article, which can't be read without signing up? :doh:


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        D Offline
        D Offline
        David A Gray
        wrote on last edited by
        #5

        The article that you cited, about the in modifier is very explicit in it summary.

        David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

        1 Reply Last reply
        0
        • D David A Gray

          When I saw mention of compiler support for ReadOnlyReferences in the C# 7.2 language, for passing variables by reference but without exposing data to modifications in a list of new features in version 4.7.2 of the Microsoft .NET Framework in What’s new in Microsoft .Net Framework 4.8, I realized that this is a big deal. Here's why. * Without this option, the only way to pass a read-only reference to an object is to create an adapter, so that you can pass through the properties through getter methods, omitting setters entirely. * Writing such an adapter is time consuming and error-prone. * Using the adapter adds a layer to every request to read a property on the underlying object, because the request must pass through the getter on the adapter, which passes it along to the getter on the adapted object. * If your project has many of these, your source code tree has lots of branches that add little value, and the extra objects make the assembly bigger. See C# Futures: Read-Only References and Structs for additional details not mentioned in the InfoWorld article, not the least of which is that this parameter modifier applies to structs, too. Presumably, that includes the system-defined structs, such as DateTime, char, and others.

          David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

          M Offline
          M Offline
          megaadam
          wrote on last edited by
          #6

          Did I ever mention that I like C++?

          ... such stuff as dreams are made on

          D 1 Reply Last reply
          0
          • M megaadam

            Did I ever mention that I like C++?

            ... such stuff as dreams are made on

            D Offline
            D Offline
            David A Gray
            wrote on last edited by
            #7

            I use C++ from time to time, too, but how is that related to this thread?

            David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

            1 Reply Last reply
            0
            • D David A Gray

              When I saw mention of compiler support for ReadOnlyReferences in the C# 7.2 language, for passing variables by reference but without exposing data to modifications in a list of new features in version 4.7.2 of the Microsoft .NET Framework in What’s new in Microsoft .Net Framework 4.8, I realized that this is a big deal. Here's why. * Without this option, the only way to pass a read-only reference to an object is to create an adapter, so that you can pass through the properties through getter methods, omitting setters entirely. * Writing such an adapter is time consuming and error-prone. * Using the adapter adds a layer to every request to read a property on the underlying object, because the request must pass through the getter on the adapter, which passes it along to the getter on the adapted object. * If your project has many of these, your source code tree has lots of branches that add little value, and the extra objects make the assembly bigger. See C# Futures: Read-Only References and Structs for additional details not mentioned in the InfoWorld article, not the least of which is that this parameter modifier applies to structs, too. Presumably, that includes the system-defined structs, such as DateTime, char, and others.

              David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

              C Offline
              C Offline
              Clifford Nelson
              wrote on last edited by
              #8

              It also solves some sticky issues when you want to ensure that a method cannot modify contents of an object. It would have been a lot of work before to protect data in an object so that it could not be modified. You would have to basically deep clone it and use the clone.

              D 1 Reply Last reply
              0
              • C Clifford Nelson

                It also solves some sticky issues when you want to ensure that a method cannot modify contents of an object. It would have been a lot of work before to protect data in an object so that it could not be modified. You would have to basically deep clone it and use the clone.

                D Offline
                D Offline
                David A Gray
                wrote on last edited by
                #9

                Especially given that you may need only a subset of its methods and properties, it's often easier to create and adapter that exposes the needed methods as read only properties of itself, and forwards the required methods to the private instance which it adapts. It's usually straightforward to create a basic adapter to meet an immediate need, which can subsequently be extended to support more read only properties and/or forwarded methods.

                David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                C 1 Reply Last reply
                0
                • D David A Gray

                  Especially given that you may need only a subset of its methods and properties, it's often easier to create and adapter that exposes the needed methods as read only properties of itself, and forwards the required methods to the private instance which it adapts. It's usually straightforward to create a basic adapter to meet an immediate need, which can subsequently be extended to support more read only properties and/or forwarded methods.

                  David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                  C Offline
                  C Offline
                  Clifford Nelson
                  wrote on last edited by
                  #10

                  If an adapter makes sense, then yes you can, but that may be a lot of adapters if there are many levels that need to be created for different objects, and if adapter does not make sense, them may be increasing maintenance significantly as need to provide different access to methods and properties.

                  D 1 Reply Last reply
                  0
                  • C Clifford Nelson

                    If an adapter makes sense, then yes you can, but that may be a lot of adapters if there are many levels that need to be created for different objects, and if adapter does not make sense, them may be increasing maintenance significantly as need to provide different access to methods and properties.

                    D Offline
                    D Offline
                    David A Gray
                    wrote on last edited by
                    #11

                    All the better! If ReadOnlyReference enables you to dispense with multiple adapters, you are further ahead of the game in reducing code bloat. However, I usually prefer to extend an existing adapter over creating a new one, especially if the extension just adds a property. The exception to that rule is if that property is hidden to prevent information disclosure or leakage.

                    David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                    1 Reply Last reply
                    0
                    • D David A Gray

                      When I saw mention of compiler support for ReadOnlyReferences in the C# 7.2 language, for passing variables by reference but without exposing data to modifications in a list of new features in version 4.7.2 of the Microsoft .NET Framework in What’s new in Microsoft .Net Framework 4.8, I realized that this is a big deal. Here's why. * Without this option, the only way to pass a read-only reference to an object is to create an adapter, so that you can pass through the properties through getter methods, omitting setters entirely. * Writing such an adapter is time consuming and error-prone. * Using the adapter adds a layer to every request to read a property on the underlying object, because the request must pass through the getter on the adapter, which passes it along to the getter on the adapted object. * If your project has many of these, your source code tree has lots of branches that add little value, and the extra objects make the assembly bigger. See C# Futures: Read-Only References and Structs for additional details not mentioned in the InfoWorld article, not the least of which is that this parameter modifier applies to structs, too. Presumably, that includes the system-defined structs, such as DateTime, char, and others.

                      David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                      D Offline
                      D Offline
                      Dr Walt Fair PE
                      wrote on last edited by
                      #12

                      David A. Gray wrote:

                      lots of branches that add little value, and the extra objects make the assembly bigger

                      Don't worry, we'll never need more than 640K of RAM!

                      CQ de W5ALT

                      Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups