I'm trying write small program using c# interface concept for the area of circle & square.While Giving specific condition if (args[0] == "S")
there is an error IndexOutOfRangeException
:
if (args[0]=="S")
fig = new Square();
if (args[0]=="C")
fig = new Circle();
That will happen if args
is empty. You can't ask for the first element of an empty array, because there isn't one. You should check the length first:
if (args.Length == 0)
{
// Maybe exit? Is it valid not to specify any arguments?
}
// Either use an "else" here, or if you've quit in the "if" block
// then you don't need to because you know that there's at least
// one argument by now
See more on this question at Stackoverflow