Arraylist: Size Is Dynamically Increased As Required
Arraylist: Size Is Dynamically Increased As Required
• Example:-
ArrayList a=new ArrayList();
a.Add("chm");
a.Add(10);
a.Add("mahindra satyam");
a.Add(2.5);
foreach(object i in a)
{
System.Console.WriteLine(i);
}
AddRange() Method
• Syntax:- public virtual void AddRange( ICollection c)
• Adds the elements of an ICollection to the end of the ArrayList.
using System.Collections;
class test {
public static void Main() {
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.Add("this");
a.Add("is");
a.Add("a test");
a.AddRange(x);
for(int i=0;i<a.Count;i++)
System.Console.WriteLine(a[i]);
} }
ArrayList Indexer
• There is a read-write indexer associated with an ArrayList.
• Example:-
*See next slide
Sample Program
using System.Collections; using System;
class test {
public static void Main() {
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.Add("this");
a.Add("is");
a.Add("a test");
a.AddRange(x);
Array ta=Array.CreateInstance(typeof(object),5);
ta.SetValue("one",0);
ta.SetValue("two",1);
ta.SetValue("three",2);
a.CopyTo(3,ta,2,3);
for(int i=0;i<ta.Length;i++)
System.Console.WriteLine(ta.GetValue(i));
} }
Insert() Method
• Inserts an element into the ArrayList at the specified index.
• Syntax:- public virtual void Insert(int index, object value)
• Example:-
ArrayList a=new ArrayList();
a.Add("this");
a.Add("is");
a.Add("a test");
a.Insert(2,“an example");
for(int i=0;i<a.Count;i++)
System.Console.WriteLine(a[i]);
Output
this
is
an example
InsertRange() Method
• Inserts the elements of a collection into the ArrayList at the
specified index.
• public virtual void InsertRange( int index, ICollection c)
Example:-
int[] x={55,33,44,90};
double[] y={2.5,3.5,4.5,5.5};
ArrayList a=new ArrayList();
a.AddRange(x);
a.InsertRange(2,y);
foreach(object i in a)
System.Console.WriteLine(i);
GetEnumerator() Method
• Public virtual IEnumerator GetEnumerator()
• Example:-
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.AddRange(x);
a.RemoveRange(1,3);
ReadOnly() & FixedSize()
• public static ArrayList ReadOnly(ArrayList)
• public static ArrayList FixedSize(ArrayList)
Example:-
int[] x={55,33,44,90};
ArrayList a=new ArrayList();
a.AddRange(x);
ArrayList b=ArrayList.FixedSize(a);
ArrayList c=ArrayList.ReadOnly(a);
//b.Add(22); Error because the array list b is fixed size
//c[1]=100; Error because the array list c is read-only
IEnumerator en=b.GetEnumerator();
while(en.MoveNext()) {
System.Console.WriteLine(en.Current); }
Contains() & BinarySearch()
Contains
• Determines whether an element is in the ArrayList.
Syntax:-
public virtual bool Contains(object item)
BinarySearch
• Delegate declaration
• Delegate method definition
• Delegate instantiation
• Delegate invocation
Delegate Declaration
modifier delegate return-type delegate-name (formal-parameters);
delegate is a keyword,
Examples:-
public delegate void mydelegate ();
public delegate int delexample (int x,int y);
Delegate Method Definition
• The methods that are referenced by a delegate are known as delegate
methods.
• The signature of a delegate method must be same as that of its
delegate.
• Syntax:-
modifier delegate return-type delegate-name (formal-parameters)
{
Statements;
}
• Example:-
delegate int mydelegate(); // Delegate declaration
public int delmethod1(int x,int y){} //Delegate method 1
public int delmethod2(int x,int y){} //Delegate method 2
Delegate instantiation
• Creation of a delegate instance takes the form:
• Example:-
• Syntax:-
• Example:-
mydelegate m1=new mydelegate(t.method1);
mydelegate m2=new mydelegate(t.method2);
mydelegate m3=m1+m2; //delegate multicasting
m3() //multicasted delegate invocation
Sample Program
using System;
public delegate void mydelegate();
class test {
public void method1() {
System.Console.WriteLine( "first delagate method");
}
public void method2() {
System.Console.WriteLine( "second delagate method");
}
public static void Main() {
test t=new test();
mydelegate m1=new mydelegate(t.method1);
mydelegate m2=new mydelegate(t.method2);
mydelegate m3=m1+m2+m1+m2+m2-m1;
m3();
} }