简体   繁体   中英

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 } public string UserNames { get => UserName; set => UserName = UserNames } but that didn't seem to work.

几乎,您必须在设定者中value

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

C# <7.0

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

C# 7.0

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

You can write like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM