Delegate and Collection [Resolved]

Posted by Rajesh081725 under C# on 11/5/2012 | Points: 10 | Views : 1304 | Status : [Member] | Replies : 5
Can u any one tell me

What is delegatre and where it is used delegate give examples in .net

what is Collection and where it is used in Collection given example .net

why collection and delegate are used in .net

Time is Gold
Thanks & Regards,
Rajesh Kumar,
9962038582.



Responses

Posted by: Muhsinathk on: 11/6/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Collection
1)Collection represents a set of objects that you can access by stepping through each element in turn.
2)Object class is the base class of every type in .NET. All the collections implement IEnumerable interface that is extended by ICollection interface. IDictionary and IList are also interfaces for collection which are derived from ICollection .
3) Offering the capability to group objects in logical constructs, they improve code readability and self documentation, as well as enhance maintainability.
4)The Collection class implements the "HAS" relationship
5)For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key.
6) A generic collection enforces type safety so that no other data type can be added to it. When you retrieve an element from a generic collection, you do not have to determine its data type or convert it.
7)The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

System.Object

Object class is the base class of every type. All other types directly or indirectly derive from object class.

System.Collections.IEnumerable

It exposes the enumerator, which provides a collection like behavior to user defined classes.

System.Collections.ICollection

ICollection interface specifies a method for getting the size of collection, creating enumerators on a collection and managing synchronized access to all non-generic collections. It is a base interface for classes in the System.Collections namespace.

System.Collections.IList

a)IList interface represents the collection of objects that can be individually accessed by index.
b)The implementation of IList falls into three categories: read-only, fixed-size, and variable-size. A read only IList cannot be modified. A fixed size IList does not allow the addition or removal of elements, but it allows the modification of the existing elements. A variables size IList allows the addition, removal, and modification of elements.

System.Collections.IDictionary

It represents a collection of key/value pairs. IDictionary interface is implemented by classes that supports collections of associated keys and values.

Example:-

GetEnumerator()

It returns the enumerator object that can be used to iterate through the collection. It allows using the foreach statement. Enumerators only allow reading the data in the collection.
Collapse | Copy Code

Array array = new int[] { 12, 24, 26, 35, 40, 59 };
IEnumerator iEnum = array.GetEnumerator();

string msg = "";
while (iEnum.MoveNext())
{
int n = (int)iEnum.Current;
msg += n.ToString() + "\n";
}

MessageBox.Show(msg);

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 11/5/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object.
Please go through these links
http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
http://dev.luca.minudel.it/dotnet/usecollections/tutorial_eng.htm

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 11/6/2012 [Member] Bronze | Points: 25

Up
0
Down
Delegates
1.A delegate allows the programmer to encapsulate a reference to a method inside a delegate object.
2.Delegates allow methods to be passed as parameters.
3.Delegates can be chained together; for example, multiple methods can be called on a single event.
4.Delegates have faster flexibility of the application and code reuse
Delegates are used in the following cases:

a) Event handlers
b) Callbacks
c) LINQ
d) Implementation of design patterns
Delegate example with steps

1. Defining the delegate

public delegate int Calculate (int value1, int value2);

2. Creating methods which will be assigned to delegate object

//a method, that will be assigned to delegate objects
//having the EXACT signature of the delegate
public int add(int value1, int value2)
{
return value1 + value2;
}
//a method, that will be assigned to delegate objects
//having the EXACT signature of the delegate
public int sub( int value1, int value2)
{
return value1 - value2;
}

3. Creating the delegate object and assigning methods to those delegate objects

//creating the class which contains the methods
//that will be assigned to delegate objects
MyClass mc = new MyClass();

//creating delegate objects and assigning appropriate methods
//having the EXACT signature of the delegate
Calculate add = new Calculate(mc.add);
Calculate sub = new Calculate(mc.sub);

4. Calling the methods via delegate objects

//using the delegate objects to call the assigned methods
Console.WriteLine("Adding two values: " + add(10, 6));
Console.WriteLine("Subtracting two values: " + sub(10,4));


Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 11/6/2012 [Member] Bronze | Points: 25

Up
0
Down
Nice Explanation "Muhsinathk" it helps

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 11/6/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Rajesh,
Please mark as answer if it helpful to you..That helps others who search the same..

Rajesh081725, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response