Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is protected virtual new

Tags:

c#

I was reading a tutorial about making custom principal, and I've encountered the code:

public class BaseController : Controller
{
    protected virtual new UserPrincipal User
    {
        get { return HttpContext.User as UserPrincipal; }
    }
}

How exactly is "protected virtual new" working ?

Keyword "new" is what confuses me the most.

like image 400
Admir Tuzović Avatar asked Sep 30 '12 14:09

Admir Tuzović


People also ask

What is protected virtual?

A protected Virtual Machine is a VM that is 'isolated' (in every way possible) from the 'host' and other VMs in a virtualized environment/infrastructure.

Can you override without virtual?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

What are virtual methods?

A virtual method is one that is declared as virtual in the base class. A method is declared as virtual by specifying the keyword "virtual" in the method signature. A virtual method may or may not have a return type. Virtual methods allow subclasses of the type to override the method.

What is the use of virtual keyword?

The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class.


2 Answers

protected means that it is visible only inside this class and classes derived from it.

virtual means that it can be overriden in derived classes.

new means that here you create new overriding hierarchy, i.e. you stop overriding the method defined in the base class and replace it with this method.

More details can be found here

like image 60
Zdeslav Vojkovic Avatar answered Nov 15 '22 12:11

Zdeslav Vojkovic


new is used for member hiding.

Unlike an overridden method, a new method will not be called by a reference to the base class.

See the documentation.

like image 35
SLaks Avatar answered Nov 15 '22 13:11

SLaks