The correct way to implement the property for which property reports the error "invalid index" if user attempts to cross bounds of the

The correct way to implement the property for which property reports the error "invalid index" if user attempts to cross bounds of the array for a student class with 5 integer arrays.
A.
Code:
 class student
 {
     int []scores = new int[5] {23, 42, 54, 11, 65};
     public int this[int index]
     {
         get
         {
             if (index < 5)
             return scores[index];
             else
             {
                 Console.WriteLine("invalid index");
                 return 0;
             }
         }
         set
         {
             if (index < 5)
             scores[index] = value;
             else
             Console.WriteLine("invalid index");
         }
     }
 }
B.
Code:
 class student
 {
     int []scores = new int[5] {23, 42, 54, 11, 65};
     public int this[int index]
     {
         get
         {
             if (index < 5)
             return scores[index];
         }
     }
 }
C.
Code:
 class student
 {
     int []scores = new int[5]{23, 42, 54, 11, 65};
     public int this[int index]
     {
         set
         {
             if (index < 5)
             return scores[index];
             else
             {
                 Console.WriteLine("invalid index");
                 return 0;
             }
         }
     }
 }
D. None of the mentioned
 

Tìm kiếm

Back
Top