New () at the end of the method declaration

The master class just gave me some C # classes that I should use in a .NET application.
There is a typo that I have never seen, and I can not find any explanation on the Internet ...

Here is the code:

public void GoTo<TView>() where TView : Form, new()
{
    var view = Activator.CreateInstance<TView>();

    //si on vient de creer une startup view alors on affiche l'ancienne
    //la reference a la nouvelle sera detruite en sortant de la fonction GoTo
    if (view is StartupForm)
    {
        ShowView(_StartupForm);
    }
    else ShowView(view);

}

What is a keyword new()right at the end of a method declaration?

+4
source share
3 answers

See MSDN :

The new restriction indicates that any type argument in a general class declaration must have an open constructor with no parameters. Use a new constraint, the type cannot be abstract.

So when you say:

void myMethod<T>(T item) where T : class, new();

, T. , T (int, float, double ..). , T .

+8

. , TView .

+11

, constuctor, 10.1.5 #.

where ( new()), (Β§7.6.10.1). , ( ) , (. Β§10.1.5).

, .

0

Source: https://habr.com/ru/post/1624446/


All Articles