简体   繁体   中英

What does the angle bracket syntax mean in C#

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.

That syntax is called generics . In a nutshell (a very tiny nutshell), imagine that your app had more than 1 database (eg 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>

DropCreateDatabaseAlways is the database intializer base class. MusicStoreDB is the database which will be dropped and re-created everytime the application starts. DropCreateDatabaseAlways<MusicStoreDB>() is the code that does that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM