6

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!

Community
  • 1
  • 1
Irfarino
  • 522
  • 1
  • 8
  • 13
  • I would use a C style cast instead of the safe cast. Now throw that away and use a `List`. – Ed S. Jul 27 '12 at 03:31

4 Answers4

6

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.

Habib
  • 219,104
  • 29
  • 407
  • 436
4

Because the base class (ArrayList) also has an indexer. The new says "I'm deliberately replacing the base-class member with one of my own."

It also works without the new, but the new tells the compiler that you know what you're doing.

Of course, in code written anytime within the last seven or eight years, you should be using List<T> instead of ArrayList, and then you don't need to make a descendant with its own strong-typed indexer.

Joe White
  • 94,807
  • 60
  • 220
  • 330
4

This is because this class hides a member of the base class(ArrayList also defines the same indexer).

New Tells the compiler that this behavior is intended and not by mistake.

Read the c# polymorphism guide

Replacing a member of a base class with a new derived member requires the new keyword. If a base class defines a method, field, or property, the new keyword is used to create a new definition of that method, field, or property on a derived class. The new keyword is placed before the return type of a class member that is being replaced.

nunespascal
  • 17,584
  • 2
  • 43
  • 46
1

Usually if a basetype intends a method to be overridden, it would be marked as virtual. In which case the subtype just marks the method with override.

If the basetype does not and the subtype defines an identical method, then there is an ambiguity. The C# compiler asks you to confirm that you want this by marking the subtype method with new - in effect saying 'yes I am aware and I want to hide the method offered by the base type'

Gishu
  • 134,492
  • 47
  • 225
  • 308