Home > Enterprise >  Please guide about this error how i can solve it.Index (zero based) must be greater than or equal to
Please guide about this error how i can solve it.Index (zero based) must be greater than or equal to

Time:08-22

Index (zero based) must be greater than or equal to zero and less than the size of the argument list. I'm want to print all strings using one Console.WriteLine() method.

Student stdnt = new Student();
Console.WriteLine("\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}", stdnt.full_name, stdnt.course, stdnt.subject, stdnt.university, stdnt.e_mail, stdnt.phone);
Console.ReadKey();```

CodePudding user response:

You have 6 inputs but 7 placeholders:

Console.WriteLine("\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}", 
stdnt.full_name, // 0
stdnt.course, // 1
stdnt.subject, // 2
stdnt.university, // 3
stdnt.e_mail, // 4
stdnt.phone);// 5
// No 6

However, this is better:

// Note the $
Console.WriteLine($"\n{stdnt.full_name}\n{stdnt.course}\n{stdnt.subject}\n{stdnt.university}\n{stdnt.e_mail}\n{stdnt.phone}"); 

Please see String interpolation in C#.

CodePudding user response:

just remove \n{6} from your code; you have 6 property in your instance and try to print 7 property

  • Related