C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
In the previous chapters, we have discussed about
enums,
debugger and nested types in
C#. Let.s study a new topic called C# generics in this chapter.
Objective
The main objective of this chapter is to study C# generics and
System.Collections.Generic namespace in
C# programming.
Generics
Generics is introduced in C# 2.0 which is the mechanism of writing codes which are reusable. Another C# mechanism to achieve this is by using inheritances.
Difference between these two is that, inheritance expresses re-usability with a base type and generics uses a 'template
' which comes with 'placeholder
' types.
- By using generics, we can reduce casting and boxing and type safety is also increased.
- Maximizes the code re-usability and improves the performance.
- Generics are commonly used in creating collection classes.
- .NET Framework class library contains several generic collection classes. They reside in System.Collections.Generic namespace.
- We can also create our own generics of classes, interfaces, methods, delegates and events.
Generic types
Generic types declare the type parameters. Let's have an example of using generic types in
C#,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
class Program
{
public class MyClass<T> // Generic type
{
int pos;
T[] demo = new T[10];
public void Insert(T obj)
{
demo[pos++] = obj;
}
public T Update()
{
return demo[--pos];
}
}
static void Main()
{
MyClass<int> my = new MyClass<int>();
my.Insert(2);
my.Insert(6);
my.Insert(8);
int i = my.Update();
Console.WriteLine(i); // prints 8
int j = my.Update();
Console.WriteLine(j); // prints 6
int k = my.Update();
Console.WriteLine(k); // prints 2
}
}
}
In the above code, we have a generic type MyClass<T>. And we are creating instances for that type in the Main() method.
MyClass<int> fills in the type parameter T with type argument int
, implicitly creating a type on the fly (occurs at runtime).
In many cases, instead of creating own type, we use List<T> which is the class provided by the .NET Framework library. It represents a strongly typed list of objects which are accessed by index.
List<T> class provides methods to sort, search and manipulate lists.
The above code prints the following lines in your console,
Using generics is an awesome feature of C#.
System.Collections.Generic
System.Collections.Generic is the C# namespace introduced from C# 2.0 which comes with several generic based collection classes. It is recommended to use these classes instead of older ones (non-generic classes) such as
ArrayList.
This namespace contains classes and interfaces that defines generic collections, which helps user to create strongly typed collections with better performance and type safety.
Classes:
There are so many classes in this namespace. Some of them are,
- Dictionary<TKey, TValue>: Represents the collection of keys and values (key-value pairs).
- Dictionary<TKey, TValue>.KeyCollection: Represents the collection of keys that are present in Dictionary<TKey, TValue>.
- Dictionary<TKey, TValue>.ValueCollection: Represents the collection of values that are present in Dictionary<TKey, TValue>.
- Comparer<T>: Used in the implementations of IComparer<T> interface (generic) to provide a base class for that.
- HashSet<T>: Represents a set of values.
- EqualityComparer<T>: Used in the implementations of IEqualityComparer<T> interface (generic) to provide base class for that.
- List<T>: Provides methods to sort, search and manipulate lists. It represents a strongly typed list of objects which are accessed by index.
- Queue<T>: Represents the collection of objects in FIFO (first-in, first-out) manner.
- Stack<T>: Represents variable size collection of instances of same type in LIFO (last-in, first-out) manner.
There are some other classes such as LinkedList<T>
, LinkedListNode<T>
, SortedList<TKey, TValue>
, SortedSet<T>
, SynchronizedCollection<T>
etc.
Interfaces:
System.Collections.Generic namespace contain some interfaces. They are,
- IComparer<T>: It defines a method to compare two objects for the types.
- ICollection<T>: This is the important interface which defines the methods to manipulate the generic collections.
- IEnumerable<T>: It exposes the enumerator, which supports iteration and performs simple iterations over a generic collection.
- IEnumerator<T>: Supports iteration and allows simple iteration over a generic specified type collection.
- ISet<T>: Gives a base interface for the abstraction of sets.
- IList<T>: It represents strongly typed objects which are accessed by index.
- IDictionary<TKey, TValue>: It represents the collection of key-value pairs.
There are some other generic interfaces such as IEqualityComparer<T>
, IReadOnlyCollection<T>
, IReadOnlyList<T>
etc.
Structures:
There are some generic structures in this
namespace. They are,
- List<T>.Enumerator: It enumerates the elements of the type List<T>.
- Dictionary<TKey, TValue>.Enumerator: It enumerates the elements of type Dictionary<TKey, TValue>.
- Queue<T>.Enumerator: It enumerates the elements of the type Queue<T>.
- HashSet<T>.Enumerator: It enumerates the elements of the type HashSet<T>.
- Stack<T>.Enumerator: It enumerates the elements of the type Stack<T>.
- LinkedList<T>.Enumerator: It enumerates the elements of the type LinkedList<T>.
- SortedSet<T>.Enumerator: It enumerates the elements of the type SortedSet<T>.
- KeyValuePair<TKey, TValue>: It defines the key-value pairs that can be set or retrieved.
There are some other generic structures such as SortedDictionary<TKey, TValue>.Enumerator
etc.
Conclusion
In this article, we have studied about C# generics and
System.Collections.Generic namespace in C#. Hope you understand.
Thanks for reading,
Regards,
Krishna.