Вопрос

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?

Это было полезно?

Решение

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
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top