Frage

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?

War es hilfreich?

Lösung

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
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top