0

Hello all,
I am new to C# and learning concepts . I got this doubt and Thought i could seek guidance from some of you.

class zzz
{
public static void Main()
{
xxx a = new xxx();
a[2] = 20;
System.Console.WriteLine(a[2]);
}
}
class yyy
{
public virtual int this [ int i]
{
get
{
System.Console.WriteLine(“yyy get “ + i);
return 20;
}
set
{
System.Console.WriteLine(“yyy set “ + value + “ “ + i);
}
}
}
class xxx : yyy
{
public override int this [ int i]
{
get
{
int p = base[i];
System.Console.WriteLine(“xxx get “ + i + “ “ + p);
return 200;
}
set
{
System.Console.WriteLine(“xxx set “ + value + “ “ + i);
base[i] = value;
}
}
}

Output obtained:
xxx set 20 2
yyy set 20 2
yyy get 2
xxx get 2 20
200

why is control in set property executing base class code?
Once it goes to base class seeing virtual keyword it should search fr the override
Keyword and come back and again execute the derived class code know?
It happened first time when we used a[2] = 20. How did the flow get changed
when used base in set{ } property??

Thanks in advance.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by ddanbe
0

why is control in set property executing base class code?

You told it to do so by calling base
When a method or indexer in this case is marked virtual it can be overridden it doesn't have to be overridden.
Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.