|
|
@ -47,32 +47,30 @@ class Program
|
|
|
|
|
|
|
|
|
|
|
|
### Multiple type parameters
|
|
|
|
### Multiple type parameters
|
|
|
|
|
|
|
|
|
|
|
|
Generic classes can receive multiple types as parameters:
|
|
|
|
* Generic classes can receive multiple types as parameters:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
```csharp
|
|
|
|
class CustomContainer<T1, T2, T3>
|
|
|
|
class CustomContainer<T1, T2, T3>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public T1 First { get; set; }
|
|
|
|
|
|
|
|
public T2 Second { get; set; }
|
|
|
|
|
|
|
|
public T3 Third { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Program
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CustomContainer<int, string, DateTime> container
|
|
|
|
public T1 First { get; set; }
|
|
|
|
= new CustomContainer<int, string, DateTime>();
|
|
|
|
public T2 Second { get; set; }
|
|
|
|
container.First = 10;
|
|
|
|
public T3 Third { get; set; }
|
|
|
|
container.Second = "Testing.";
|
|
|
|
|
|
|
|
container.Third = DateTime.Now;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class Program
|
|
|
|
```
|
|
|
|
{
|
|
|
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
CustomContainer<int, string, DateTime> container
|
|
|
|
|
|
|
|
= new CustomContainer<int, string, DateTime>();
|
|
|
|
|
|
|
|
container.First = 10;
|
|
|
|
|
|
|
|
container.Second = "Testing.";
|
|
|
|
|
|
|
|
container.Third = DateTime.Now;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Creating a generic Method
|
|
|
|
### Creating a generic Method
|
|
|
|
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
```csharp
|
|
|
|
|
|
|
|
|
|
|
|
void GenericMethodExample<T>(T value)
|
|
|
|
void GenericMethodExample<T>(T value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Console.WriteLine
|
|
|
|
Console.WriteLine
|
|
|
@ -83,7 +81,7 @@ GenericMethodExample<string>("ABC");
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
* ***Note:*** You could name the generic type as anything, e.g. `<GenericType>`. It is named `<T>` by convention.
|
|
|
|
* ***Note:*** You could name the generic type as anything, e.g. `<GenericType>`. It is named `<T>` here by convention.
|
|
|
|
|
|
|
|
|
|
|
|
## Exercise 1: Initializing a populated list
|
|
|
|
## Exercise 1: Initializing a populated list
|
|
|
|
<!-- _class: exercise -->
|
|
|
|
<!-- _class: exercise -->
|
|
|
|