Tag Archives: Arguments

Optional Arguments

The definition of a method, constructor, indexer or delegate can specify that a parameter is optional. Any caller must provide all required arguments but does not have to specify optional ones.

Each optional parameter has a default value as part of its definition that is used if an argument for it is not specified.

public void MyFunction(int parameterOne, string optionalPerameter = "Hello World")
{
}

In the example above the string Hello World will be passed into the function if the caller decides not to pass in an argument for that parameter.

Optional parameters are defined at the end of the argument list, after any required arguments. If the caller provides an argument for any optional parameter it must provide arguments for all previous optional parameters.

NSubstitute – Capturing Arguments Passed To A Function

Consider the situation where you are writing a unit test and need to find out what the value of an argument was that was passed to a function. This can be achieved using NSubstitute.

public interface MyInterface
{
    void MyFunction(int parameterOne);
}

public class MyClass : MyInterface
{
    public void MyFunction(int parameterOne)
    {
    }
}

public class Foo
{
    private MyInterface _myClass;

    public Foo(MyInterface myClass)
    {
        _myClass = myClass;
    }

    public void DoWork()
    {
        _myClass.MyFunction(20);
    }
}

Looking at the code above, we are passing the value 20 into MyFunction but we want to unit test that 20 is the value that actually gets passed in. So how do we do that?

[Test]
public void TestThat20IsPassedToMyFunction()
{
    MyInterface myClass = Substitute.For<MyInterface>();

    Foo foo = new Foo(myClass);

    int result = 0;

    myClass.MyFunction(Arg.Do<int>(arg => result = arg));

    foo.DoWork();

    Assert.AreEqual(20, result);
}

Firstly we mock an instance of MyInterface using NSubstitute so that we can determine when functions are called on it. Then we use the Arg.Do statement to capture the passed in arguments to MyFunction and store them in the result variable. Finally its just a simple matter of calling the function we want to test and asserting that the value in result is 20.

Named Arguments

Named arguments enable you to specify an argument for a particular parameter by associating the argument with the parameters name rather than with the parameters position in the parameter list.

Normally arguments are specified in the order that they appear in the parameter list as shown below.

public class Program
{
    private static void Main(string[] args)
    {
        int argumentA = 10;
        double argumentB = 2.5;

        double result = MyFunction(argumentA, argumentB);
    }

    private static double MyFunction(int paramterA, double paramaterB)
    {
        return paramaterB + paramterA;
    }
}

Named arguments free you from the need to remember to look up the order of parameters in the parameter list of called methods. Arguments can be specified in any order by using the name of the parameter.

public class Program
{
    private static void Main(string[] args)
    {
        int argumentA = 10;
        double argumentB = 2.5;

        double result = MyFunction(paramaterB: argumentB, paramterA: argumentA);
    }

    private static double MyFunction(int paramterA, double paramaterB)
    {
        return paramaterB + paramterA;
    }
}

A named argument can follow a positional argument as shown below.

public class Program
{
    private static void Main(string[] args)
    {
        int argumentA = 10;
        double argumentB = 2.5;

        double result = MyFunction(argumentA, paramaterB: argumentB);
    }

    private static double MyFunction(int paramterA, double paramaterB)
    {
        return paramaterB + paramterA;
    }
}

But a positional argument cannot follow a named argument, attempting to do this would cause a compilation error.