Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
By
SRIRAM . B
Overview
Collections
Indexers
Delegates
Operator Overloading
Collections
Collections are objects that can contain arrays of other objects and contain
functionality that controls access to these objects.
Collection classes are generally used for maintains list of objects and may expose
additional functionality over arrays.
IList – Provides list of items for collection as well as capability for accessing
these elements.
Types of Collections
Stack -> L I F O
Queue -> F I F O
ArrayList -> Array is whose size can increase and decrease dynamically. It can
hold items of different types. It can increase and decrease size dynamically you don't
have to use REDIM keyword . You can access any item in array using the INDEX
value of the array position.
Hashtable -> You can access array using INDEX value of array, but how many times
you know the real value of index. Hashtable provides way of accessing the index using
a user identified KEY value, thus removing the INDEX problem.
Console.ReadLine();
}
}
}
Example – Queue Collection
using System;
using System.Collections;
namespace Collections
{
class sampleQueue
{
static void Main(string[] args)
{
Queue numbers = new Queue();
// fill the queue
foreach (int number in new int[4]
{ 9, 3, 7, 2 })
{
numbers.Enqueue(number);
Console.WriteLine(number + " has
joined the queue");
}
Example – Queue Collection..
Console.ReadLine();
}
}
}
Example - Hashtable
using System;
using System.Collections;
namespace Collections
{
class sampleHash
{
static void Main(string[] args)
{
Hashtable ages = new Hashtable();
// fill the SortedList
ages["John"] = 41;
ages["Diana"] = 42;
ages["James"] = 13;
ages["Francesca"] = 11;
Example - Hashtable
// iterate using a foreach statement
// the iterator generates a DictionaryEntry object
containing a key/value pair
foreach (DictionaryEntry element in ages)
{
string name = (string)element.Key;
int age = (int)element.Value;
Console.WriteLine("Name: {0}, Age: {1}", name,
age);
}
Console.ReadLine();
}
}
}
Example – Array List
using System;
using System.Collections;
namespace Collections
{
class arrayList2
{
static void Main(string[] args)
{
ArrayList numbers = new ArrayList();
// fill the ArrayList
foreach (int number in new int[12] { 10, 9,
8, 7, 7, 6, 5, 10, 4, 3, 2, 1 })
{
numbers.Add(number);
}
// remove first element whose value is 7
(the 4th element, index 3)
numbers.Remove(7);
// remove the element that's now the 7th
element, index 6 (10)
numbers.RemoveAt(6);
Example – Array List..
//iterate remaining 10 elements using a for
statement
for (int i = 0; i != numbers.Count; i++)
{
int number = (int)numbers[i];
// Notice the cast
Console.WriteLine(number);
}
// iterate remaining 10 using a foreach statement
foreach (int number in numbers)
// No cast needed
{
Console.WriteLine(number);
}
Console.ReadLine();
}
}
}
Example – Sorted List
using System;
using System.Collections;
namespace Collections
{
class sampleSortedList
{
static void Main(string[] args)
{
SortedList ages = new SortedList();
// fill the SortedList
ages["John"] = 39;
ages["Diana"] = 40;
ages["James"] = 12;
ages["Francesca"] = 10;
Example – Sorted List..
}
}
}
Indexers
You can use an indexer to access members of a class as if it was an array.
Indexers are nameless and use a reserved word this for implementation.
To read and write values, the get and set accessors are used.
Example :-
– obj[0] = “test”
– Console.WriteLine(“{0}”, obj[0]);
Indexers..
Array & Indexers
Indexers can use non-integer subscripts and atleast one subscript should be
provided
Can be passed more than one parameter to the indexer (mainly used for 2D
array)
Example – Indexers
public class Index
{
public static void Main()
{
//Create a new IndexTest object a
IndexTest a = new IndexTest();
Delegates are reference types that derive from the common base class:
System.Delegate.
All methods invoked by the same delegate must have the same parameters and
return value
To use a delegate
Defining
– public delegate void mydelegate( );
– Defines a delegate for methods that return no value and takes no
parameters
Instantiating
– After defining it instantiate it and refer to a method
– myclass ed1 = new myclass( );
– mydelegate callback = new mydelegate (ed1.method);
To call a Delegate
callback();
Example - Delegates
class DelegateTest
{ static void func()
{ //Static method
System.Console.WriteLine(“DelegateTest.func”
);
}
static void Main(){
//Static method on the class
//Instantiation
SimpleDelegate d = new
SimpleDelegate(func);
// Invocation
d();
} }
Operator Overloading
Overloading operators that act upon objects
These method names are operatorop where op specifies exactly which operator is
being overloaded. Ex: operator+
All arithmetic operators return an instance of the class and manipulate objects of
the same class.
Operator Overloading..
Relational operators
must be paired
If you overload = = and != operators, then you should override the Equals method,
such that if both returns true in a same situation
}
static void Main(string[] ar)
{
Complex num1 = new Complex(5, 6);
Complex num2 = new Complex(2, 4);
Complex sum = num1 + num2;
Complex prod = num1 * num2;
Example – Operator Overloading..
Console.WriteLine("Number1 = {0} + {1}i",
num1.real, num1.imaginary);
Console.WriteLine("Number2 = {0} + {1}i",
num2.real, num2.imaginary);
Console.WriteLine("Sum = {0} + {1}i",
sum.real, sum.imaginary);
Console.WriteLine("Product = {0} + {1}i",
prod.real, prod.imaginary);
Console.ReadLine();
}
}
}
Session Ends
Exercise
Relax