C# Programming - Properties - Discussion

Discussion :: Properties - General Questions (Q.No.4)

4. 

If a class Student has an indexer, then which of the following is the correct way to declare this indexer to make the C#.NET code snippet given below work successfully?

Student s = new Student(); 
s[1, 2] = 35;

[A].
class Student
{ 
    int[ ] a = new int[5, 5]; 
    public property WriteOnly int this[int i, int j]
    { 
        set
        { 
            a[i, j] = value;
        } 
    }
}
[B].
class Student
{ 
    int[ , ] a = new int[5, 5]; 
    public int property WriteOnly
    { 
        set
        { 
            a[i, j] = value;
        } 
    } 
}
[C].
class Student
{ 
    int[ , ] a = new int[5, 5];
    public int this[int i, int j] 
    {
        set
        { 
            a[i, j] = value;
        } 
    } 
}
[D].
class Student
{ 
    int[ , ] a = new int[5, 5];
    int i, j; 
    public int this
    { 
        set
        { 
            a[i, j] = value;
        } 
    } 
}

Answer: Option C

Explanation:

No answer description available for this question.

Vlad said: (Sep 27, 2014)  
No A - We have to declare 2 dimensional array correctly.
No B - There is no this keyword in the property.
No D - No numbers in the property.

Swash said: (Oct 23, 2015)  
Why this keyword is required?

Ahmed said: (Mar 25, 2016)  
Why is it required?

Bhumi said: (Sep 1, 2016)  
Yes, I also agree to @Swash question.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.