Generics in C#
What is Generics?
Generic is also a class used to store similar values according to the specified datatype. Generics are called as type safe. According to the given datatype only the similar data will be stored. The predefined generics are existing in System.Collections.Generic namespace. Generics in C# are similar to templates in C++.
It is used to store similar values according to the specified datatypes.
1. Example of List:
2. Example of List using Property:
It is used to store values in Key/value pair. For key/value datatype should be given.
Example of Dictionary:
var is a predefined structure used to work with any type of data. It is used to access values from Dictionary generic. It contains key/value property. By using them key/value can be accessed from generic.
In this way, we learned Generics in C# and some basic programs which will help beginners to quick start with Generics.
You may also be interested in...
- Slack Integration with C#
- Xamarin Forms: Getting Started
- Part 1: Introduction to C#
- Part 2: Arrays and Function in C#
- Part 3: OOPs Concepts in C#
- Part 4: Constructor/Destructor in C#
- Part 5: Abstract class in C#
- Part 6: Indexer,Delegates, Anonymous Method, Lambda expression in C#
- Part 7: Collections in C#
- Part 8: Generics in C#
- Part 9: Attributes in C#
- Part 10: Sealed/Partial class in C#
- Part 11: MultiThreading in C#