3

I am working on a function that I would like to define more than one prototype and was wondering if this is possible.

I know visual studios does this for some of their function calls but wasn't sure if it was something that was possible to do on my own.

Charles
  • 50,943
  • 13
  • 104
  • 142
Ian Oswald
  • 1,365
  • 3
  • 16
  • 27
  • 2
    Could you give a code example of what you are trying to do? – Maciej Jun 28 '13 at 14:19
  • Addition to the answers below: default parameter value is a .NET 3.5 C# feature. I see you are using VS2010. http://stackoverflow.com/questions/3602234/c-sharp-default-parameters – Csaba Toth Jun 28 '13 at 18:17

2 Answers2

4

Yes you can overload a function:

public void Function() {

}

public void Function(string argument) {

}

public void Function(string argument, string argument) {

}

Or you can use params:

public void Function(params string[] arguments) {

}

You can then invoke the function via:

 Function("argument1", "argument2", "argument3");
Darren
  • 68,902
  • 24
  • 138
  • 144
  • This is perfect, exactly what I wanted to know, Thanks. As soon as it lets me, I'll select this as the correct answer. – Ian Oswald Jun 28 '13 at 14:26
0

As Darren wrote, you can create overloads in C#

With the introduction of optional parameters and default values, some of these can be reduced to a single implementation:

For instance,

 public void Foo(string param1, int param2)
 {
   ...
 }

 public void Foo(string param1)
 {
   Foo(param1, 12);
 }

Can be replaced by

 public void Foo(string param1, int param2 = 12)
 {
   ...
 }
vc 74
  • 37,131
  • 7
  • 73
  • 89