How to use arraylist in c#.
theses are basically collection so we need to import system.Collection;
Then to add array and array list
Here i have shown the code and examples to include array list in ur application.
private void arraylistdefault()
{
ArrayList myValue = new ArrayList { "MyFirstValue", "2", "3" };
int indexofmyva = myValue.IndexOf("MyFirstValue");
// Response.Write(indexofmyva);//to get index of MyFirstValue string
//myValue.RemoveAt(indexofmyva); //to to removce MyFirstValue
//using Index
myValue.Insert(3, "Third Value"); // inserting Third Value string
myValue.Insert(4, "insert4");
myValue.Add("add5");
string[] ek = { "5col-name", "6-col-name" }; //adding range
//it will insert 5col-name at 6 th position and 6-col-name at 6th position
//myValue.Remove("added5"); //will remove string added5
//myValue.RemoveAt(3); will remove word which occupies value of index number 3;
myValue.InsertRange(5,ek);
myValue.Insert(7, "addedT7");
myValue.RemoveAt(6); // will remove index 6th value
myValue.Insert(6, ">new"); // will add >new string on 6th position
myValue.SetRange(7, ek);
myValue.GetRange(3, 2);
//myValue.Add(3); //Unable to cast object of type 'System.Int32' to type 'System.String'.
even we can add int type data but we need to do casting like boxing and unboxing.
foreach(string strvalue in myValue)
{
Response.Write(strvalue +"<br/>");
}
}
Now add array in array List.
Now here we are going to take int string value and wanted to convert in integer .
ArrayList new_array = new ArrayList();
new_array.AddRange(mystrarray);
int forty = Convert.ToInt32("40");
new_array.Add(forty);
theses are basically collection so we need to import system.Collection;
Then to add array and array list
Here i have shown the code and examples to include array list in ur application.
private void arraylistdefault()
{
ArrayList myValue = new ArrayList { "MyFirstValue", "2", "3" };
int indexofmyva = myValue.IndexOf("MyFirstValue");
// Response.Write(indexofmyva);//to get index of MyFirstValue string
//myValue.RemoveAt(indexofmyva); //to to removce MyFirstValue
//using Index
myValue.Insert(3, "Third Value"); // inserting Third Value string
myValue.Insert(4, "insert4");
myValue.Add("add5");
string[] ek = { "5col-name", "6-col-name" }; //adding range
//it will insert 5col-name at 6 th position and 6-col-name at 6th position
//myValue.Remove("added5"); //will remove string added5
//myValue.RemoveAt(3); will remove word which occupies value of index number 3;
myValue.InsertRange(5,ek);
myValue.Insert(7, "addedT7");
myValue.RemoveAt(6); // will remove index 6th value
myValue.Insert(6, ">new"); // will add >new string on 6th position
myValue.SetRange(7, ek);
myValue.GetRange(3, 2);
//myValue.Add(3); //Unable to cast object of type 'System.Int32' to type 'System.String'.
even we can add int type data but we need to do casting like boxing and unboxing.
foreach(string strvalue in myValue)
{
Response.Write(strvalue +"<br/>");
}
}
Now add array in array List.
Now here we are going to take int string value and wanted to convert in integer .
ArrayList new_array = new ArrayList();
new_array.AddRange(mystrarray);
int forty = Convert.ToInt32("40");
new_array.Add(forty);
0 comments: