Would it be correct use of the new modifier in this context?
Technically - yes, there will no errors or warnings.
As for me, using of new keyword itself as a modifier indicates a design drawback.
I'll give one example.
public class MyList<T> : List<T>
{
public int AddCallsCount;
public new void Add(T t)
{
AddCallsCount++;
base.Add(t);
}
}
[TestClass]
public class Test
{
[TestMethod]
public void TestThatsNotGood()
{
List<object> list = new MyList<object>();
list.Add(1);
list.Add(2);
MyList<object> myList = list as MyList<object>;
Assert.AreEqual(0, myList.AddCallsCount);
}
}
It looks like polymorphism works, but actually does not.
UPDATE:
Ok, there is very simplified explanation. I omit explanation of what polymorphism is.
Polymorphims is realized with implementation of abstract\virtual
and overriding
methods. As soon as neither virtual
nor override
modifiers are specified MyList<T>.Add
is just another 'common' public method. And with MyList<T>
inherited List<T>
, MyList<T>.Add
'hides' List<T>.Add
because name and parameters of both methods are same.
At lower level: as soon as List<T>
type definition of method Add
isn't marked with virtual
keyword, compiler won't search for overriding methods of actual instance type (MyList<T>
in this certain case) for variable of given type (List<T>
in this certain case).
Definetely it may lead to logic errors and incorrect usage of class API.
Hence, compiler 'thinks' that probably there is a logical mistake or design drawback and warns programmer. The new
keyword is just a way to talk to the compiler
yes, I know that it's not good, but I need it because of my bad design
.