-2

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 "";
 }
eslamirad
  • 1
  • 5
  • 4
    You can't. Why would you want to do that? – 15ee8f99-57ff-4f92-890c-b56153 Oct 18 '19 at 20:00
  • 2
    What are you trying to accomplish with this? – Dortimer Oct 18 '19 at 20:01
  • 2
    What is the value in making this change? What goal are you trying to achieve? –  Oct 18 '19 at 20:01
  • GetNationalCode(List nameList) i assume, and then foreach(var name in nameList) { ... } ? At Least i think that is your question but i am not sure. (your locals should not start with capitals by the way) | and where do you get Users from is that a this.Users ? – edelwater Oct 18 '19 at 20:01
  • 2
    Perhaps you should take a look at this : https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/using-indexers – Jonas W Oct 18 '19 at 20:02
  • 2
    What you're describing, an indexed *non-default* property, is possible in VB.NET but not in C#. In C#, it's emulated by returning an object that has an indexer (indexed *default* property), as germi's answer says. You could write it in VB.NET, although it doesn't use square brackets, and then in C#, you'd be calling `get_GetNationalCode(name)` (although you'd probably remove `Get` so it ends up being `get_NationalCode(name)`). – madreflection Oct 18 '19 at 20:32

1 Answers1

3

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).

germi
  • 4,628
  • 1
  • 21
  • 38
  • 1
    What led you to that conclusion and how do indexers tie into a static method? – Rotem Oct 18 '19 at 20:12
  • 1
    @Rotem I'm guessing OP is misunderstanding what square brackets are generally used for, so this would probably be the place to start. – Dortimer Oct 18 '19 at 20:14
  • @Rotem They don't. It just seemed to me that that's what the OP was looking for. That they are not possible as static is a good point that I should mention in the answer, though. – germi Oct 18 '19 at 20:14