Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "default" generic constraint do?

The following code compiles, but it seems that Microsoft's docs don't mention this particular constraint type at all.

class TestGenericsBase<T1>
{
    public virtual void Method1<T>(T arg)
    {
    }
}

class TestGenerics : TestGenericsBase<object>
{
    public override void Method1<T>(T arg)
        where T : default // what does this do?
    {
    }
}

Any idea what does it do? The only clue I have so far is that it only works on methods.

like image 718
user626528 Avatar asked Mar 22 '21 20:03

user626528


People also ask

What is a generic constraint?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.

What does the generic constraint of type interface do?

Interface Type Constraint You can constrain the generic type by interface, thereby allowing only classes that implement that interface or classes that inherit from classes that implement the interface as the type parameter.

What does the generic constraint of type interface do in C#?

C# allows you to use constraints to restrict client code to specify certain types while instantiating generic types. It will give a compile-time error if you try to instantiate a generic type using a type that is not allowed by the specified constraints.

What does where T new () mean?

where T : new() Means that the type T must have a parameter-less constructor. Having this constraint will allow you to do something like T field = new T(); in your code which you wouldn't be able to do otherwise.

What is SQL server default constraint?

SQL Default Constraint is used to assign default values to the SQL table columns. In general, every column accepts either Nulls or a Value. If the user forgot to enter the value, then SQL Server will assign a NULL value to the column. In those cases, we can use the SQL Server Default constraint to replace those Nulls with the default value.

What is the default constraint for nullable generic parameters?

This default constraint can be found in C#9 design proposal for nullable reference types. It was added to disambiguate between class in struct constraints in overriden or explicitly implemented methods for nullable generic parameters T?. More details also can be found in the Unconstrained type parameter annotations proposal

How to insert system values using the default constraint?

The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE (): CREATE TABLE Orders (ID int NOT NULL, OrderNumber int NOT NULL,

What if I don’t know the default constraint name?

If you don’t know the Default constraint name, use the first SELECT Statement to get all the Constraint name.


1 Answers

This default constraint can be found in C#9 design proposal for nullable reference types. It was added to disambiguate between class in struct constraints in overriden or explicitly implemented methods for nullable generic parameters T?.

More details also can be found in the Unconstrained type parameter annotations proposal

To allow annotations for type parameters that are not constrained to reference types or value types, C#9 allows a new where T : default constraint.

These constraints and annotations are working within nullable contexts

like image 87
Pavel Anikhouski Avatar answered Oct 21 '22 03:10

Pavel Anikhouski