@@ -44,7 +44,7 @@ Generic constraints specify the requirements that a type parameter must satisfy.
4444
4545Without constraints, the compiler cannot assume anything about ` T ` .
4646
47- ## Basic Syntax
47+ ### Basic Syntax
4848
4949``` CSharp
5050class MyClass <T > where T : ConstraintType
@@ -67,41 +67,41 @@ Constraint order must follow this pattern:
67673 . Interfaces
68684 . new()
6969
70- ## Common Constraint Types
70+ ### Common Constraint Types
7171
72- ### Reference type constraint
72+ #### Reference type constraint
7373
7474``` CSharp
7575where T : class
7676```
7777
78- ### Value type constraint
78+ #### Value type constraint
7979
8080```CSharp
8181where T : struct
8282```
8383
84- ### Parameterless constructor
84+ #### Parameterless constructor
8585
8686```CSharp
8787where T : new()
8888```
8989
90- ### Inherit from a base class
90+ #### Inherit from a base class
9191
9292```CSharp
9393where T : BaseClass
9494```
9595
96- ### Implement an interface
96+ #### Implement an interface
9797
9898```CSharp
9999where T : IDisposable
100100```
101101
102- ## Why Constraints Are Needed
102+ ### Why Constraints Are Needed
103103
104- ### Calling members that may not exist
104+ #### Calling members that may not exist
105105
106106```CSharp
107107void CloseItem<T>(T item)
@@ -117,7 +117,7 @@ void CloseItem<T>(T item) where T : IDisposable
117117 }
118118```
119119
120- ### Creating instances of T
120+ #### Creating instances of T
121121
122122``` CSharp
123123class Factory <T >
@@ -133,7 +133,7 @@ class Factory<T> where T : new()
133133 }
134134```
135135
136- ## Method-Level Constraints
136+ ### Method-Level Constraints
137137
138138Constraints can also be applied to individual methods:
139139
@@ -143,9 +143,3 @@ public void Save<T>(T item) where T : IEntity
143143 item .Store ();
144144}
145145```
146-
147- ## Summary
148-
149- - Constraints define what a type parameter must support.
150- - They ensure type safety for operations involving ` T ` .
151- - Common constraints include: class, struct, new(), base classes, and interfaces.
0 commit comments