0

Can someone tell me what New() in the following method constructor means? I know that TEvent inherits from CompositePresentationEvent but what does new() mean?

protected void RaiseEvent<TEvent, TParameter>(TParameter parameter) where TEvent : 
    CompositePresentationEvent<TParameter>, new()
    {
        EventAggregator.GetEvent<TEvent>().Publish(parameter);
    }
Phil
  • 42,255
  • 9
  • 100
  • 100
MikieIkie
  • 80
  • 6
  • 1
    Note that this method is not a constructor: **1.** You would not specify a return type with a constructor. **2.** You would not specify generic type parameters with a constructor; you would specify them at the type level. – stakx - no longer contributing Mar 29 '13 at 23:01

2 Answers2

4

In that function declaration new() is a constraint on the TEvent generic type parameter. It means the type must be have a public constructor without parameters.

Read more about constraints on generic types here.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • 3
    Close - it means that the type can be created and must have a parameterless constructor. You are thinking of the `class` constraint. – Matt Johnson-Pint Mar 29 '13 at 22:55
  • From the link you provided: "The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last." – Phil Mar 29 '13 at 22:57
1

That is a constraint on the generic parameter of your method. It basically means that the generic type being passed as an argument to your method must have a parameterless constructor.

MeTitus
  • 3,390
  • 2
  • 25
  • 49