简体   繁体   中英

pass parameter to C# method with [Square brackets]

how to pass parameter between [Name]
please see

 public static string GetNationalCode(string Name)
 {
            foreach(var User in Users)
            {
                if(User.Name== Name)
                {
                    return User.Code;
                }
            }
            return "";
  }

I want to convert the top method to this code

  public static string GetNationalCode[string Name]()
  {
            foreach(var User in Users)
            {
                if(User.Name== Name)
                {
                    return User.Code;
                }
            }
            return "";
 }

I believe what you are looking for are Indexers .

As @Rotem points out in the comments though, indexers are not possible as static methods (as discussed here for example ).

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