Generics is a very interesting and handy topic in C#, It is also very popular question that’s being asked in interviews. In this article we will learn about basic concept of generics and its usage.
What is generics ? 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”
In simple definition, we can say that Generics is a mechanism in C#, which allows to write type safe, reusable and high performance code.
This is an extension to the article on nullable types, If you haven’t gone through the article then please have a look here .
Null Coalescing Operator (??) – This is a C# operator that is basically used to set the default value of a variable, Its takes two operands, If the left operand is null, then the right operand is returned else the left operand.
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.
Boxing – Its is a process of converting a value type variable to a reference type variable. That means a value type variable (int, float, bool etc) gets converted in to a reference type variable (object).
In below code snippet, a value type int x converts to a reference type as object obj.
int x = 100; //Value type
object obj = new object(); //Reference type
obj = x; //Boxing
If you haven’t gone through my previous article on Reference type and value type then please 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 int i = 0, Now you want to assign null to it at run-time. If you write i = nullthen 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 int X = 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.
String s = "Hello"
It tells to the compiler that string is a data type that will be used to hold text value.
int i = 123
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.