Trương Duy Bằng
New member
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.
B.
C.
D. None of the mentioned
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");
}
}
}
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];
}
}
}
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;
}
}
}
}