Class ObjectCollection
Represents a strongly typed collection of objects that can be accessed by index. This class provides a basic implementation of a dynamic array.
Initializes a new instance of the ObjectCollection
class that is empty and has the default initial capacity.
Initializes a new instance of the ObjectCollection
class that is empty and has the specified initial capacity.
public ObjectCollection(int capacity);
-
capacity
int
- The number of elements that the new list can initially store.
void
Appends a value to the end of the ObjectCollection
.
public void Add(object value);
-
value
object
- The object value to append to the end of theObjectCollection
.
void
Removes all objects from the ObjectCollection
.
public void Clear();
Determines whether an element is in the ObjectCollection
.
public bool Contains(object value);
-
value
object
- The object to search for in theObjectCollection
.
Return Value
true
if the element is found in the ObjectCollection
; otherwise, false
.
Gets the zero-based index of the first occurrence of the specified Object
within the entire ObjectCollection
.
public int IndexOf(object value);
-
value
object
- TheObject
to locate in theObjectCollection
.
Return Value
The zero-based index of the first occurrence of the specified Object
within the entire ObjectCollection
, or -1 if the object is not found.
Inserts an element into the ObjectCollection
at the specified index.
public void Insert(int index, object value);
-
index
int
- The zero-based index at whichvalue
should be inserted. -
value
object
- TheObject
to insert into theObjectCollection
.
int
Gets the number of elements actually contained in the ObjectCollection
.
public int Count { get; }
Return Value
The number of elements actually contained in the ObjectCollection
.
Object
Gets or sets the element at the specified index.
public object this[int index] { get; set; }
-
index
int
- The zero-based index of the element to get or set.
Return Value
The element at the specified index. If the property is being set, the value of the element at the specified index.
Remarks
The ObjectCollection
class is a lightweight implementation of a dynamic array that can store any type of object. It is derived from CollectionBase
, providing basic collection management functionality. This class is useful when you need a collection that can grow dynamically and allows access to its elements by index.
It supports adding, inserting, and retrieving elements by index, as well as checking for the existence of an element and clearing the collection. The capacity of the collection automatically increases as needed.