Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the angle bracket syntax mean in C#

Tags:

c#

asp.net-mvc

I am reading this book, and it tries to use initializer to Create the DB each time the application runs, so the code snippet is like this:

protected void Application_Start() {
    Database.SetInitializer(new DropCreateDatabaseAlways<MusicStoreDB>());

    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

I can't understand this part:

 new DropCreateDatabaseAlways<MusicStoreDB>()

What is this syntax? what does <MusicStoreDB>() mean?

I know it's not a fancy question, but I need help here.

Thanks.

like image 930
Brittany Rutherford Avatar asked Jul 25 '15 07:07

Brittany Rutherford


1 Answers

That syntax is called generics. In a nutshell (a very tiny nutshell), imagine that your app had more than 1 database (e.g. MusicStoreDB, MovieStoreDB, etc), you could use the same DropCreateDatabaseAlways class with the different db types. In other words, generics let you define classes and functions that can act on many different types, for example

List<int>, List<string>, List<MyAwesomeClass>

like image 67
Roly Avatar answered Sep 27 '22 22:09

Roly