-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGenerics.cs
35 lines (32 loc) · 1.09 KB
/
Generics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* A generic collection enforces type safety so that other data types cannot be added to it.
* When we retrieve an item from a generic collection, we do not need to cast it to the appropriate data type.
* Implemented under the System.Collections.Generic namespace.
*
* Classes present in the System.Collections.Generic namespace are:
* Stack<T>, Queue<T>, List<T>, Dictionary<TKey, TValue>, SortedList<>, LinkedList<T>, SortedSet<T>, etc.
*
* <T> is a placeholder for the data type that will be used with the generic collection.
*/
using System;
using System.Collections.Generic;
namespace Basics
{
class Generics
{
public void GenericList()
{
// empty list can be created as:
//List<int> numbers = new List<int>();
List<int> numbers = new List<int> { 1, 2 };
numbers.Add(3);
numbers.Add(4);
numbers.Add(5);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Console.WriteLine($"Count: {numbers.Count}");
}
}
}