Skip to content

Proposal: Default Generic Type Parameter #6248

Closed
@alrz

Description

@alrz

The ability to specify a default type for a generic type parameter:

class Foo<TBar = Bar, TBaz = Baz> {}

As a real world example, the IdentityDbContext class in ASP.NET Identity:

class IdentityDbContext<TUser = IdentityUser, TRole = IdentityRole, TKey = string, TUserLogin = IdentityUserLogin, TUserRole = IdentityUserRole , TUserClaim = IdentityUserClaim> where ...

to avoid the need to declare different subtypes for different default types like

class IdentityDbContext : IdentityDbContext<IdentityUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> { ... }
class IdentityDbContext<TUser> : IdentityDbContext<TUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> where TUser : IdentityUser { ... }

In companion with improved type aliases can avoid this for god's sake.

To use default type parameters, one may skip the type parameter as follow:

var foo = new Foo<,>(); // new Foo<Bar, Baz>()
var foo = new Foo<A,>(); // new Foo <A, Baz>()

This has some interactions with generic type argument inference proposals like #5429 or #2319.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions