Question

I am new to programming and I don't know how to check if an argument exists. For example, if an argument such as args[2] does not exist then run some code else do something else. Is there a means to achieve this?

Était-ce utile?

La solution

Assuming "not exists" means that the args[2] would return a Index out of range exception, check the length of the args array:

if (args.Length == 3)
{
    //Do stuff since args[2] exists
}
else
{
    //Do something else
}

If you mean args[2] is null, then just check that

if (args[2] != null)
{
    //Do stuff since args[2] exists
}
else
{
    //Do something else
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top