Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Friday, March 18, 2011

Generic

using System.Collections.Generic;

In an Arraylist, As Each item is stored as an object, During Sorting and traversing the list, it must be typed at runtime, and then compared for sorting/searching wheareas Generic list has all items typed compile time, which saves a lot of workload during sorting and searching.

The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface using an array whose size is dynamically increased as required.

Type safety : it gives error at compile time if a data type mismatch is found.

Code reusablity : Same code is reused for various data tyoe like int,string etc.

public class User
{
protected string name;
protected int age;
protected double salary;
public string Name { get { return name; } set { name = value; } }
public int Age { get { return age; } set { age = value; } }
public double Salary { get { return salary; } set { salary = value; } }

static void Main(string[] args)
{
List users = new List();

for (int x = 0; x < 5; x++)
{
User user = new User();
user.Name ="Name : "+ "subhash" + x;
user.Age = x+20;
user.salary =x + 12500;
users.Add(user);
}

foreach (User user in users)
{
System.Console.WriteLine(System.String.Format("{0}:{1}:{2}", user.Name, user.Age,user.salary));
}

Console.ReadLine();
}
}

No comments:

Post a Comment