Delrog
Delrog

Reputation: 765

Getter and Setter Aliasing in C#

How would I go about aliasing a variable from it's plural form so that referencing the plural form changes or returns the single form?

I have a global variable as such with a Getter and Setter:

public string UserName { get; set; }

And would like to set up an aliased variable UserNames that would return UserName and when set would modify UserName?

I've tried public string UserNames { get => UserName; set => UserName = UserNames } but that didn't seem to work.

Upvotes: 0

Views: 550

Answers (3)

amin
amin

Reputation: 581

You can write like this:

private string UserName;
public string UserNames
{
  get
  {
    return UserName;
  }  
  set
  {
    UserName=value;
  }
 }  

Upvotes: 1

adjan
adjan

Reputation: 13674

C# <7.0

public string UserNames { get { return UserName; } set { UserName = value; }}

C# 7.0

public string UserNames { get => UserName; set => UserName = value; }

Upvotes: 4

Igor
Igor

Reputation: 62228

Almost, you have to use value in your setter.

public string UserNames { get => UserName; set => UserName = value; } 

Upvotes: 5

Related Questions