Monday, April 20, 2020

Performance List.Item[Int32] Property is O(1) operation

List.Item[i] is O(1) operation

Reason : There is a very large data list and suppose system wants to lookup for 9999 some element in the list. The operation performed to located is one step that is current location+ 9999= 0+9999

Gets or sets the element at the specified index.
C#
public T this[int index] { get; set; }
List accepts null as a valid value for reference types and allows duplicate elements.
This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index].
Retrieving the value of this property is an O(1) operation; setting the property is also an O(1) operation.

No comments :