Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Properties cannot be passed by reference. Here are a few ways you can work around this limitation.</p> <h2>1. Return Value</h2> <pre><code>string GetString(string input, string output) { if (!string.IsNullOrEmpty(input)) { return input; } return output; } void Main() { var person = new Person(); person.Name = GetString("test", person.Name); Debug.Assert(person.Name == "test"); } </code></pre> <h2>2. Delegate</h2> <pre><code>void GetString(string input, Action&lt;string&gt; setOutput) { if (!string.IsNullOrEmpty(input)) { setOutput(input); } } void Main() { var person = new Person(); GetString("test", value =&gt; person.Name = value); Debug.Assert(person.Name == "test"); } </code></pre> <h2>3. LINQ Expression</h2> <pre><code>void GetString&lt;T&gt;(string input, T target, Expression&lt;Func&lt;T, string&gt;&gt; outExpr) { if (!string.IsNullOrEmpty(input)) { var expr = (MemberExpression) outExpr.Body; var prop = (PropertyInfo) expr.Member; prop.SetValue(target, input, null); } } void Main() { var person = new Person(); GetString("test", person, x =&gt; x.Name); Debug.Assert(person.Name == "test"); } </code></pre> <h2>4. Reflection</h2> <pre><code>void GetString(string input, object target, string propertyName) { if (!string.IsNullOrEmpty(input)) { prop = target.GetType().GetProperty(propertyName); prop.SetValue(target, input); } } void Main() { var person = new Person(); GetString("test", person, nameof(Person.Name)); Debug.Assert(person.Name == "test"); } </code></pre>
 

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