0

i have a function like this :

public void MyMethod(string param1=null, int? param2=null, Datetime? param3=null)
{
     //do something
}

So when i call it:

MyMethod(param1,null,null) or MyMethod(null,param2,null) ...

but i don't like the way above, can i call it as :

MyMethod(param1) or MyMethod(param2) or MyMethod(param3)
2
  • What happened when you tried? Please show your actual code, tell us in precise detail what happened, and what you wanted to happen instead. Se stackoverflow.com/help/mcve Commented Nov 7, 2014 at 2:06
  • I have a method search products with 3 parameters, each times i call it, i have to pass full 3 parameters although it null, but i want to simple code, what way to call it with one or two param? Commented Nov 7, 2014 at 2:13

2 Answers 2

1

You can do it like this, same type parameters would be a problem in overloading, but i do favor overloading rather than this approach

Call it like this:

public void Main(string[] args)
{
    MyMethod(param2: value);
    MyMethod(param1: value);
    MyMethod(param3: value);
}

public void MyMethod(string param1=null, int? param2=null, Datetime? param3=null)
{
     //do something
}
Sign up to request clarification or add additional context in comments.

5 Comments

@Nguyễn Huy, try this.
Ok, thank you, i learn a lot today, but this way vs overloading, which is better?
Overloading is better because you can control how many of the parameters must be passed in. In @RonaldEstacion example it is possible to call the underlying method directly without passing in any parameters at all - this will not be detected by the compiler as being incorrect.
@NguyễnHuy because in overloading, just like greg said, you can control the parameters and it's much more cleaner than doing this. Say for instance, you have pre-task to do if you invoke MyMethod wile passing param1, you can control it there. And if you have many parameters, i suggest wrapping it in a class.
thank you so much for your explain, easy to understand.
1

You can do this with overloads - i.e. having more than one method called MyMethod. They will need to have different signatures though, meaning parameters of different types.

So you'd have:

public void MyMethod(string param1)
{
     MyMethod(param1, null, null);
}

public void MyMethod(int param2)
{
     MyMethod(null, param2, null)
}

You will also need your original method - which the other methods call into.

2 Comments

Thank you so much for your answer, but in case method have params with same type?i really want to know
Unfortunately the compiler will throw a syntax error because it's unable to determine which method you are actually calling. You could get around this by giving one method a different name and then calling your base method, but that might not meet your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.