If you haven’t gone through my previous article on generics then please go through it here.
In my previous article, we discussed about the type safety and code re-usability feature of generics, continuing on that, in this article we will discuss about the performance improvement generics brings in to our code.
How usage of generics improves performance ?
Generics can bring performance improvement in terms of speed and memory consumption as follows.
With generics in our program, we don’t need to use type casting, that eliminates the extra work compiler does to check the type at run-time. This certainly improves the performance of the application.
Another benefits generics bring is that, it eliminates the possibility of boxing and un-boxing in most of the situations, which generally improves the performance and reduces the memory consumption.
Generics is a very interesting and powerful feature in C# 2.0, It provides a type safety and reusable programming framework. In this article, we will learn about the basic concept of generics, its benefits and the usage.
What is generics ?
In simple definition, we can say that generics is a mechanism in C#, which allows to write type safe, reusable and high performance code.
In other words, generics allows us to design classes, methods, Interface and delegates with a specific data type. The data type information is passed to the generic type during declaration and instantiation time.
This enables in developing common classes, methods, Interface and delegates, which helps in the application performance, improves the productivity and provides the type safety feature.
This is what Microsoft says about generics.
“Generics are the most powerful feature of C# 2.0. Generics allow you to define type-safe data structures, without committing to actual data types. This results in a significant performance boost and higher quality code, because you get to reuse data processing algorithms without duplicating type-specific code”
Before going through Boxing and Unboxing concept in C#, it’s important to know about value type and reference type variables. Please go through my article here.
It is the process of converting a value type variable to a reference type variable (object). That means the C#.Net converts a value type variable of type (int, float, bool etc) to an object which is a reference type variable.
In below code snippet, we have declared a value type
intx and then performing a boxing operation that converts the value type
x to an object
obj which is a reference type.
Nullable Types in C#.Net, Before going through in details. It is important to know Reference types and Value Types. If you haven’t gone through my previous article on Reference type and value type then ,you can go through here.
What is a nullable Type?
Nullable types are special data type in C#.net, to which you can assign null value.
Lets say you have declared a variable as
inti=0, Now you want to assign null to it at run-time. If you write
i=null then compiler throws error as shown below. Now question arises How this can be achieved ?
In C#.net, all data types are mainly classified in to two categories.
Value Type :
A value type holds the value itself in a stack. For example, when we declare a value type variable as intX=500 then the C# compiler allocates some memory space from the stack and stores the value, as shown in the image.
What happens when we assign one value type to another ?
If we assign a value type variable to another value type variable then the value is copied, and in that case both of the variables work independently and any change to any of the variable doesn’t affect the other. (more…)
Data types or variable are an important thing in any programming language. It is the basic starting point for a programmer. It generally tells to the compiler the type of value it can hold.
In below example we have declared a string variable in C#.net as.
Strings="Hello" . It tells to the compiler that string is a data type that holds a text value.
In above example, we have declared a integer type in C#.net. It tells to the compiler that
int is a type that will be used to hold numbers.
Data types has some specific size and length. If we declare a type and at run time if the value it holds exceeds the max limit that the type can hold then we get the data overflow issues. So its very important to have proper data types in our program. Continue reading →
The idea behind this blogging website came to me on a fine Saturday morning. As I was lying down on my bed and lazily browsing Facebook, then accidentally I stumbled upon an article on blogging. The article was all about “How you can blog in 100 different ways ? “. It was quite interesting to learn how people from different interest, technology share their knowledge and experience to the larger audience.