Possible Duplicate:
“new” keyword in property declaration
Pardon me if this is C# 101, but I am trying to understand the code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Generics
{
class RabbitCollection : ArrayList
{
public int Add(Rabbit newRabbit)
{
return base.Add(newRabbit);
}
//collections indexer
public new Rabbit this[int index]
{
get { return base[index] as Rabbit; }
set { base[index] = value; }
}
}
}
Why does the indexer have new
in front of it? By the way, Rabbit is a class defined in another file. Thanks!
Try removing new from the code and you should get a warning:
RabbitCollection.this[int]' hides inherited member 'System.Collections.ArrayList.this[int]'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
You may want to see new Modifier C#
The new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.