Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing properties by reference in C#

I'm trying to do do the following:

GetString(     inputString,     ref Client.WorkPhone)  private void GetString(string inValue, ref string outValue) {     if (!string.IsNullOrEmpty(inValue))     {         outValue = inValue;     } } 

This is giving me a compile error. I think its pretty clear what I'm trying to achieve. Basically I want GetString to copy the contents of an input string to the WorkPhone property of Client.

Is it possible to pass a property by reference?

like image 784
yogibear Avatar asked Sep 10 '09 00:09

yogibear


People also ask

Are properties passed by reference C#?

Properties are not variables. They're methods, and cannot be passed to ref parameters. You can't use the ref , in , and out keywords for the following kinds of methods: Async methods, which you define by using the async modifier.

What is pass by value and pass by reference in C#?

In C#, arguments can be passed to parameters either by value or by reference. Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment.

Is object passed by reference C#?

By default, C# does not allow you to choose whether to pass each argument by value or by reference. Value types are passed by value. Objects are not passed to methods; rather, references to objects are passed—the references themselves are passed by value.

How do you pass a ref variable in C#?

A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters. The reference parameters represent the same memory location as the actual parameters that are supplied to the method.


1 Answers

Properties cannot be passed by reference. Here are a few ways you can work around this limitation.

1. Return Value

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"); } 

2. Delegate

void GetString(string input, Action<string> setOutput) {     if (!string.IsNullOrEmpty(input))     {         setOutput(input);     } }  void Main() {     var person = new Person();     GetString("test", value => person.Name = value);     Debug.Assert(person.Name == "test"); } 

3. LINQ Expression

void GetString<T>(string input, T target, Expression<Func<T, string>> 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 => x.Name);     Debug.Assert(person.Name == "test"); } 

4. Reflection

void GetString(string input, object target, string propertyName) {     if (!string.IsNullOrEmpty(input))     {         var 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"); } 
like image 192
Nathan Baulch Avatar answered Oct 01 '22 10:10

Nathan Baulch