Dmitriy
Dmitriy

Reputation: 937

optional parameters by [Optional] C#, check args passed

I have class constructor like this:

public Script(string scriptName, [Optional] ICollection<Tuple<string, bool>> internalFunctions, [Optional] long randomIdentifier) { }

So, how it possible to check, were something passed in randomIdentifier or internalFunctions? So, was

new Script("test1")

or

new Script("test1", null)

or

new Script("test1", null, 1)

called?

Thank you.

Upvotes: 1

Views: 284

Answers (1)

Grant Winney
Grant Winney

Reputation: 66449

If a value was not explicitly passed, then those parameters will have their default values, and you'll have to test for those values yourself:

public Script(string scriptName, [Optional] ICollection<Tuple<string, bool>> internalFunctions, [Optional] long randomIdentifier)
{
    if (internalFunctions != null)
    {
        // do something that needs internalFunctions to have a value
    }

    if (randomIdentifier != 0)
    {
        // do something that needs randomIdentifier to have a valid value
    }
    else
    {
        // either a value wasn't passed, or the value 0 was passed...
        //   you can't be sure, so you might want to make this nullable
    }
}

Upvotes: 1

Related Questions