Export (0) Print
Expand All

Collection.SetItem Method

Replaces the element at the specified index.

Namespace: System.Collections.ObjectModel
Assembly: mscorlib (in mscorlib.dll)

protected:
virtual void SetItem (
	int index, 
	T item
)
protected void SetItem (
	int index, 
	T item
)
protected function SetItem (
	index : int, 
	item : T
)
Not applicable.

Parameters

index

The zero-based index of the element to replace.

item

The new value for the element at the specified index. The value can be a null reference (Nothing in Visual Basic) for reference types.

Exception typeCondition

ArgumentOutOfRangeException

index is less than zero.

-or-

index is greater than Count.

Collection accepts a null reference (Nothing in Visual Basic) as a valid value for reference types and allows duplicate elements.

This method is an O(1) operation.

The following code example shows how to derive a collection class from a constructed type of the Collection generic class, and how to override the protected InsertItem, RemoveItem, ClearItems, and SetItem methods to provide custom behavior for the Add, Insert, Remove, and Clear methods, and for setting the Item property.

The custom behavior provided by this example is a Changed notification event that is raised at the end of each of the protected methods. The Dinosaurs class inherits Collection<string> (Collection(Of String) in Visual Basic) and defines the Changed event, which uses a DinosaursChangedEventArgs class for the event information, and an enumeration to identify the kind of change.

The code example calls several properties and methods of Collection to demonstrate the custom event.

No code example is currently available or this language may not be supported.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft