What is Generics in .NET Part - 1

Sunday, February 22, 2009

What is Generics ?

The name gives you little bit of idea that why it must be so lets start upon what are advantages of using generics while coding in .Net technology.
When we look at the term "generic", unrelated to the programming world, it simply means something that is not tied to any sort of brand name. For example, if we purchase some generic dish soap, soap that has no brand name on it, we know that we are buying dish soap and expect it to help us clean our dishes, but we really don't know what exact brand (if any) will be inside the bottle itself. We can treat it as dish soap even though we don't really have any idea of its exact contents.

Generics is to facilitate code reuse, especially the reuse of algorithms, C# includes a feature called generics. Just as methods are powerful because they can take parameters, classes that take type parameters have significantly more functionality as well, and this is what generics enable. Like their predecessor, templates, generics enable the definition of algorithms and pattern implementations once, rather than separately for each type. However, C# implements a type-safe version of templates that differs slightly in syntax and greatly in implementation from its predecessors in C++ and Java. Note that generics were added to the runtime and C# with version 2.0.

Think of Generics in this manner. We can refer to a class, where we don't force it to be related to any specific Type, but we can still perform work with it in a Type-Safe manner. A perfect example of where we would need Generics is in dealing with collections of items (integers, strings, Orders etc.). We can create a generic collection than can handle any Type in a generic and Type-Safe manner. For example, we can have a single array class that we can use to store a list of Users or even a list of Products, and when we actually use it, we will be able to access the items in the collection directly as a list of Users or Products, and not as objects (with boxing/unboxing, casting).

What is Generic type

Lets create a very simple generic class and demonstrate how it can be used as a container for a variety of other Types. Below is a simple example of what a generic class could look like:

public class Col
{
T t;
public T Val { get { return t; } set { t = value; } }
}


The class name "Col" is our first indication that this Type is generic, specifically the brackets containing the Type placeholder. This Type placeholder "T" is used to show that if we need to refer to the actual Type that is going to be used when we write this class, we will represent it as "T". Notice on the next line the variable declaration "T t;" creates a member variable with the type of T, or the generic Type which we will specify later during construction of the class (it will actually get inserted by the Common Language Runtime (CLR) automatically for us). The final item in the class is the public property. Again, notice that we are using the Type placeholder "T" to represent that generic type for the type of that property. Also notice that we can freely use the private variable "t" within the class.

What is Generic Collections

The .NET team has provided us with a number of generic collections to work with in the the latest version of the .NET Framework. List, Stack, and Queue are all going to be implemented for us. Let's see an example on how to use the provided Generic List class.
public class User
{
protected string name;
protected int age;
public string Name { get { return name; } set { name = value; } }
public int Age { get { return age; } set { age = value; } }
}
Above Class can be use as

public class UserData
{
public static void Main(string[] args)
{
System.Collections.Generic.List users = new System.Collections.Generic.List();
for (int x = 0; x < user =" new" name = "Rob" age =" x;" x =" 0;">

Which will give Output as
Rob0:0
Rob1:1
Rob2:2
Rob3:3
Rob4:4
press enter
Rob0:0
Rob1:1
Rob2:2
Rob3:3
Rob4:4

This is just basic of Generics we will discuss more about it in our upcoming articles


Read more...

Animal Farm

Tuesday, February 17, 2009

Animal Farm

Power corrupts, absolute power corrupts absolutely.

In Oct 1917, a major power-shift took place in the political arena of Russia due to Soviet movement esp. the October revolution (also known as Bolshevik revolution). Animal farm, reflects those events that occurred in then Russian politics, during Stalin era. Following the death of Vladimir Lenin in 1924, Stalin, the Soviet Leader, prevailed in a power struggle over Leon Trotsky, who was expelled from the Communist Party and deported from the Soviet Union.

This book was first rejected for publication because of the camaraderie between Russian and British at that time. It was later chosen by Time Magazine as one of the 100 best English-language novels.

The story starts with a revolution driven by animals to dethrone the humans (The owner of the farm, Mr Jones in the book) in hope of building a new future. They have a belief that this will bring prosperity to animals, they will be treated equally , able to work according to their capacity and will enjoy equal luxury. But this doesn’t last too long when one section of animal turns dictators from simple comrades.

Then the series of events takes place which shows how the ideologies of a society can be changed/twisted by those, who are in power. These are more or less similar to Russian revolution principles that were twisted and abandoned by Stalin. The 2 central characters (Pigs) resemble Stalin and Trotsky where Snowball (a pig), which is of a less political will , is overthrown by cunning malicious Napoleon (another pig).

Orwell has succeeded up to a large extent in depicting one of the most complex subject in the simplest way. The book has been written in such a way that even a novice reader wont find difficult, giving it a go. The sequence and the events of the book are those which we see/experience in our day to day life, as how a big population is governed by a bunch of people (can say politicians) and how they misuse the power by bending/denting the laws of society/country .

Quoting an example from the book, one of the major principles of the animal revolution was :- "All animals are equal" which with time was changed by the dictators as :-

ALL ANIMALS ARE EQUALS. SOME ANIMALS ARE MORE EQUAL THAN OTHERS.

Give it a try just as another story of Aesop's Fables.

Read more...

Back to TOP