Monday, April 15, 2013

Collection initializer in c sharp


Collection initializers are extension of object initializers.It look like an array.To create a collection initializers ,Auto-implemented properties are required.To work with this,a generic data type called as list is required.Generic data type holds any type of data. These are similar to c++ templates and will be indicated with <>

Example:
Code in General declaration
class result
{
public int marks {set; get;}
public string sname {set; get;}
public int id {set; get;}
}
Code for btn click
{
list<result> rs= new list<result>
{
new result{marks=100,sname="siva",id=1}
new result{marks=100,id=2}
}
foreach(result r,int rs)
Messagebox.Show(r1.marks+" "+r1.sname+" "+r1.id);
}
In the above i have taken one button to done this example
Post a Comment

Bel