I found the following code snippet in the DbSet Class of the EntityFramework:
public new Type GetType()
{
return base.GetType();
}
I have no idea why the base method is hidden, all the base classes have the method implemented calling base.
This is object.GetType()
:
[SecuritySafeCritical]
[__DynamicallyInvokable]
[MethodImpl(MethodImplOptions.InternalCall)]
public extern Type GetType();
This is in the DbQuery
class:
/// <inheritdoc />
[EditorBrowsable(EditorBrowsableState.Never)]
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
public new Type GetType()
{
return base.GetType();
}
And this is in the DbSet
(DbSet<TEntity> : DbQuery<TEntity>
) class:
/// <inheritdoc />
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
[EditorBrowsable(EditorBrowsableState.Never)]
public new Type GetType()
{
return base.GetType();
}
Why or when would you use the new
keyword and then call the base implementation?
Let's consider the alternatives.
Don't override or new anything
Problem: There's no code on which to apply attributes.
Just override it
Problem: GetType
is not virtual.
So you're left using new
with a call to base
.
The real question is what is so important about [EditorBrowsable(EditorBrowsableState.Never)]
that they went to all this trouble? My only guess is that they felt that developers would confuse GetType
with ElementType
in intellisense.
I think one reason you would use the new keyword is when you want to change the access modifier. If your method is protected in the base class you can make it public in your child class using the new keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With