Note that there are some explanatory texts on larger screens.

plurals
  1. PODesign of method using ref to alter partially immutable class
    text
    copied!<p>In my project I have a header class that represents a globally unique key for a piece of information inside the system, such as who it belongs to, what time it exists for, etc. Inside the same header class I also have fields for information that is specific to a given instance of data, such as who created this version of the information, when it was created, if its new data that needs to be saved to the database, etc.</p> <p>Here is an example of stocking some information into a data transport class and querying it back out.</p> <pre><code>var header = new IntvlDataHeader( datapoint: Guid.NewGuid(), element: Guid.NewGuid(), intervalUtc: DateTime.Now.Date); package.StockData_Decimal(header, 5m); decimal cloneData; package.TryGetData_Decimal(ref header, out cloneData); // header now refers to a different object, that could have different flags/information </code></pre> <p>Note how I made the TryGetData_Decimal pass the header variable by reference. IntvlDataHeader is a class, and if the data is found inside TryGetData then the reference is changed to point to a new instance of IntvlDataHeader that has the specific instance information in addition to having the same unique key information.</p> <p>Is combining a key with instance specific information and using a ref parameter as both in and out a bad design? Would the effort of splitting out another class so that there would be two out parameters and no ref parameters be better or avoid any potential problems?</p> <p>The signature of the method is <code>public bool TryGetData_Decimal(ref IntvlDataHeader header, out decimal data)</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload