Yes, Collections are preferred then arrays, why because we have few advantages.
1. no need to bother about element adding,element deleation...ect
2. no need to bother about memory allocation.
3. a little bit of performance and iteration also
and we have Collections also Index based Collections and key/value pair collections.
Index based collections implements ----> IList interface
Example: ArrayList
interface IList : ICollection, IEnumerable
{
bool IsFixedSize { get; };
bool IsReadOnly { get; };
int Add(object value);
object this[int index] { get; set; }
}
we can access an element using index
like
ArrayList arr = new ArrayList();
arr.Add("abc");
arr.Add("def");
arr.Add("jhi");
Console.WriteLine(arr[1]);-------> index based access
key/value based collections implements---->IDictionary interface.
Example : SortedList
interface IDictionary : ICollection, IEnumerable
{
bool IsFixedSize { get; };
bool IsReadOnly { get; };
ICollection Keys { get; }
ICollection Values { get; }
object this[object key] { get; set; }
void Add(object key, object value);
}
1. no need to bother about element adding,element deleation...ect
2. no need to bother about memory allocation.
3. a little bit of performance and iteration also
and we have Collections also Index based Collections and key/value pair collections.
Index based collections implements ----> IList interface
Example: ArrayList
interface IList : ICollection, IEnumerable
{
bool IsFixedSize { get; };
bool IsReadOnly { get; };
int Add(object value);
object this[int index] { get; set; }
}
we can access an element using index
like
ArrayList arr = new ArrayList();
arr.Add("abc");
arr.Add("def");
arr.Add("jhi");
Console.WriteLine(arr[1]);-------> index based access
key/value based collections implements---->IDictionary interface.
Example : SortedList
interface IDictionary : ICollection, IEnumerable
{
bool IsFixedSize { get; };
bool IsReadOnly { get; };
ICollection Keys { get; }
ICollection Values { get; }
object this[object key] { get; set; }
void Add(object key, object value);
}
No comments:
Post a Comment