-1

I'm working with a C# opensource project called Merchello. I'm trying to customise it it a little bit but I don't think I fully understand some of the code.

There is an existing MVC controller that I may need to change but I don't fully understand how its put together -

I admit I am not to familiar with generics in C# - can someone give me an explanation of what this signature means in particular I would like to understand what TBillingAddress means. If you could also point me to any links that would help me understand this more.

public abstract class CheckoutAddressControllerBase<TBillingAddress, TShippingAddress> : CheckoutControllerBase
    where TBillingAddress : class, ICheckoutAddressModel, new()
    where TShippingAddress : class, ICheckoutAddressModel, new()
{
Ayo Adesina
  • 2,231
  • 3
  • 37
  • 71
  • Have you tried to google this information? Here is an article from msdn: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters – Fabjan Feb 06 '19 at 17:37
  • 1
    Possible duplicate of https://stackoverflow.com/questions/713438/explain-generics-in-layman-style-in-c –  Feb 06 '19 at 17:37
  • 1
    Possible duplicate of https://stackoverflow.com/questions/4560890/what-are-generics-in-c –  Feb 06 '19 at 17:38

1 Answers1

2

For TBillingAddress:

  • class - Means the type must be a reference type

  • ICheckoutAddressModel - Means the type must implement this interface

  • new() - Means the type must have a paramterless constructor

These all constrain what types can be used for TBillingAddress and constrain what you can do with the type in any method using it.

And TShippingAddress has the same constraints.

Also see:

JSteward
  • 6,833
  • 2
  • 21
  • 30