Category Archives: C# Tutorial

C# Generic Dictionary Tutorial with Example

In this chapter you will learn

  1. What are Generic Dictionary Class?
  2. How to declare it?
  3. Programming Example

What are Generics Dictionary Class?

Dictionary is a collection of keys and Values. We bind values with keys and store them in dictionary and later that key is used for accessing dictionary value. Generic Dictionary is type safe and avoid boxing and unboxing and provides complete benefit of Dictionary. Generic Dictionary belongs to System.Collection.Generic namespace.

Generic Collection doesn’t allow inserting duplicate keys.

Important Methods of Generic Dictionary Class


Properties Usage
Comparer Gets the IEqualityComparer that is used to determine equality of keys for the dictionary.
Count Gets the number of key/value pairs contained in the Dictionary.
Item[TKey] Gets or sets the value associated with the specified key.
Keys Gets a collection containing the keys in the Dictionary.
Values Gets a collection containing the values in the Dictionary.


Methods Usage
Add(TKey, TValue) Adds the specified key and value to the dictionary.
Clear() Removes all keys and values from the Dictionary.
ContainsKey(TKey) Determines whether the Dictionary contains the specified key.
ContainsValue(TValue) Determines whether the Dictionary contains a specific value.
Equals(Object) Determines whether the specified object is equal to the current object.(Inherited from Object.)
Finalize() Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)
GetEnumerator() Returns an enumerator that iterates through the Dictionary.
GetHashCode() Serves as the default hash function. (Inherited from Object.)
GetObjectData(SerializationInfo, StreamingContext) Implements the System.Runtime.Serialization.ISerializable interface and returns the data needed to serialize the Dictionary instance.
GetType() Gets the Type of the current instance.(Inherited from Object.)
MemberwiseClone() Creates a shallow copy of the current Object.(Inherited from Object.)
OnDeserialization(Object) Implements the System.Runtime.Serialization.ISerializable interface and raises the deserialization event when the deserialization is complete.
Remove(TKey) Removes the value with the specified key from the Dictionary.
ToString() Returns a string that represents the current object.(Inherited from Object.)
TryGetValue(TKey, TValue) Gets the value associated with the specified key.

Programming Example


Star Wars
[sci-fi, Star Wars]
[Action, The One]
[Fantacy, Star Dust]
[Computer, Algorithm]
[Love, 50 First Dates]
Key found. Value is Star Wars


In this chapter you learned Generic Dictionary class with complete programming example. In the next chapter you will learn Generic SortedList.

C# Generic Lists with Complete Programming Example

In this chapter you will learn

  1. What are Generic Lists?
  2. How it is different from Collection List
  3. Programming Example

What are Generic Lists

The Generic Lists are similar to arrays. You can add, delete, populate, sort Generic Lists. is a placeholder for data types and replaced by datatype at compile time.

How it is different from Collection Lists?

However, function of the both lists are same but Generic Lists are Type Safe because it is strongly bound by data type. Unlike Collection lists you don’t need to boxing and unboxing Generic Lists.

How to declare Generic Lists

You can declare Generic Lists in following manner:

List intList = new List();

Important Function of Lists

Property Usage
Items Gets or sets the element at the specified index
Count Returns the total number of elements exists in the List
Method Usage
Add Adds an element at the end of a List.
AddRange Adds elements of the specified collection at the end of a List.
BinarySearch Search the element and returns an index of the element.
Clear Removes all the elements from a List.
Contains Checks whether the speciied element exists or not in a List.
Find Finds the first element based on the specified predicate function.
Foreach Iterates through a List.
Insert Inserts an element at the specified index in a List.
InsertRange Inserts elements of another collection at the specified index.
Remove Removes the first occurence of the specified element.
RemoveAt Removes the element at the specified index.
RemoveRange Removes all the elements that match with the supplied predicate function.
Sort Sorts all the elements.
TrimExcess Sets the capacity to the actual number of elements.
TrueForAll Determines whether every element in the List matches the conditions defined by the specified predicate.

Programming Example


Steven Clark Jack
After Sorting
Clark Jack Steven
Removing Clark
Jack Steven
Inserting Mathew at index position 2
Jack Steven Mathew


In this chapter you learned Generic Lists with complete and easy programming example. In the next chapter you will learn Generic Dictionary class with complete programming example.

C# Generics Programming

In this chapter you will learn

  • What are Generics
  • How to Write Generics Program in C#
  • Benefit of Generics in C#

What are Generics

In simple words, Generics are the that type of classes which has PalceHolder instead of datatypes. When you create generic classes you don’t specify its data type; datatypes are defined when you create objects. This makes Generic classes reusable and type-safe and your Generic classes and methods are able work with any datatypes.

More about Generics

  1. Generics improves code usability, type safety and performance.
  2. Mostly Generics are used for creating collection classes.
  3. You can use Generics by adding System.Collections.Generic namespace.
  4. You can create your own Generic interfaces, classes, methods, events and delegates.

Declare Generics Class

Use Generics in Programming

Programming Example


Printing Integer Value
Printing String Value
Hello String

Here, I have wrote the very simple program and hope you will understand it very easily. In the above program I have created a Generic class GenClass which has a placeholder. This class also has a method GetFunction that accepts a T type parameter and prints on the console. is a placeholder that is replaced by datatype when created objects.


In this chapter you learned What are Generics in C# Programming, Its benefit and complete programming exmaple.

C# Polymorphism Practice Question


Qu 1. Write a program of function overloading that has a function Convert() that accepts a value in parameter and convert them into string and print on screen.
Hint: Convert(int num);
Convert(Double num);
Convert(Fload num);
Qu 2. Write a program using Virtual and Override keyword that does the following tasks.

  1. A virtual function Engine() that has basic properties of engine like Power of engine, RPM, no of Cylinder etc.
  2. This function should be overridden in child class according to function.

Programming Example of C# Polymorphism


Qu 1. Write a program to save data to database. There should be a function Save() that accepts one, two or three values as parameter and then save that to database.


No Data to Saved
Jack saved
Jack and 22 are saved on 19-11-2016
Qu 2: Write a program using virtual and override keyword in which base class should have a function TyreSize() that should be implemented in derived class using override keyword.


Default Tyre Size is 13 Inches
Tyre Size is Overriden and new Tyre Size is : 14 Inches


In this chapter you learned Polymorphism using some programming example. In the next chapter there are some programming questions. You must solve that question using programming techniques.

Dynamic Polymorphism in C# with Programming Example

In this chapter you will learn:

  • What is Runtime Polymorphism?
  • How to implement it in program?

What is Runtime Polymorphism?

Runtime Polymorphism is also known as Dynamic Polymorphism, Late Binding, Method overriding etc. Whereas in static polymorphism we overload a function; in dynamic polymorphism we override a base class function using virtual or override keyword. Method overriding means having two or more methods with the same name, same signature but with different implementation.

Programming Example


Class : Icecream
Class : Chocolate
IceCream Type : Chocolate


  1. If base class function is marked as virtual then it is compulsory to add override keyword in derived class function.
  2. Virtual method allows child class to their own implementation of derived methods.
  3. Virtual method cannot be declared as private.

You can also learn more about virtual method and override keyword here:


In this chapter you learned Runtime Polymorphism implementation using Virtual and Override keyword. In the next chapter some programming examples are there that will help you to understand polymorphism clearly.

Function Overloading Tutorial C# with Programming Example

In this chapter you will learn:

  • What is Function Overloading?
  • Function Overloading with Programming Example

Function Overloading

In function overloading, a function works differently based on parameters. A single function can have different nature based on number of parameters and types of parameters. For example, you have a function Sum() that accepts values as parameter and print their addition. You can write multiple function with name Sum() but the parameter signature must be different.

Programming Example


No Value Provided
Sum of 5 and 4 is 9
Sum of 9.3 and 8.6 is 17.9
Hello World – is not a numeric value


When you are doing method overloading, must remember following guidelines:

  1. Methods must be different based on their signature like number of parameters and types of parameters.
  2. There is no limit for method overloading. You can write multiple methods with same name but the signature must be different.


In this chapter you learned about Function Overloading in C#. In the next chapter you will learn about Operator Overloading in C# with programming example.

Overview of Polymorphism – C#

In this chapter you will learn:

  • What is Polymorphism?
  • Types of Polymorphism in c#

What is Polymorphism?

Polymorphism is one of the fundamental concept and strongest pillars of Object Oriented Programming. Simply Polymorphism means – One Interface, Multiple Functions. It is a Greek word that means Many Forms.

Types of Polymorphism

There are two types of Polymorphism in C#.

  1. Static Polymorphism or Compile Time Polymorphism (Early Binding or Method Overloading)
  2. Dynamic Polymorphism or Runtime Polymorphism (Late Binding or Method Overriding)

Static Polymorphism

In static Polymorphism, you bind a function with an object during compile time. In simple word, you tag a function with an object while writing program. It is also called early binding. In static polymorphism, Function Overloading gets implemented.

Dynamic Polymorphism

It is also called as Late Binding or Method Overriding or Dynamic Polymorphism. In dynamic polymorphism, we override base class function using virtual & override keywords.


In the next chapter you will learn about Static Polymorphism or Function Overloading with complete programming example.

C# Event Handling Exercises

Qu: Write a program for Online Attendance. The conditions are as follow:

  1. User provides their name as Input and then application show message to “Welcome to their Name”.
  2. Jack, Steven and Mathew are banned for the organization. So, when any user enters Jack, Steven and Mathew as user name, the application raised an event and fire alarm as well as sends email to administration.



C# Built in Delegates with Programming Example

In this chapter you will learn

  • How events return a value?
  • Built in delegates in c#
  • Programming Example

Built in Delegates in C#

In the previous chapter we learned to create custom events in c#. In this chapter you will know how to use in built delegates in c#. Microsoft provides 2 built in delegates to work with.

These built in delegates helps you to write event handling code with ease. With these delegates you can pass one or more than one value to event handler. When you raise event you must follow the discipline and pass required parameters to delegates.

Example 1:

***Event Executed : This is Odd Number***


In this program, I have used built in delegates in c#.

  1. Declare built in delegates
  2. Raised Event

    this denotes the current instance of class
    EventArgs.Empty tells that there is empty value in parameter.
  3. Initialize Events in Main function
  4. Calls the methods with required parameters.

In the above program the events doesn’t return any value. Now, look for the next modified program that returns a value with EventArgs.

Example 2


Event Executed : 9 is an Odd Number


Let’s understand this example.

    1. I have created a custom OddNumberEventArgs class that inherits the EventArgs. This class simply set the parameter value to sum variable.
    2. Now, created event ev_OddNumber using this class.
    3. Pass required parameter when called this event.
    4. Now in the EventMessage() function; OddNumberEventArgs e has a value.


In this chapter you learned about built in delegates and also learned how to return a value using custom EventArgs class. In the next chapter there are some easy exercise questions that will help you to understand event handling more clearly.