In this guide, we will explore the functionality of the SortedList.IndexOfValue(Object) Method in C#, including its syntax, parameters, and sample illustrations.
What is the SortedList.IndexOfValue(Object) Method?
The IndexOfValue function provides the zero-based index of the initial occurrence of a given value within a SortedList instance.
Syntax:
It has the following syntax:
public virtual int IndexOfValue (object? value);
The method accepts two parameters: "value" and "object". When the function is called, it searches the sorted list ("object") for the "value" parameter. If the "value" is present in the list, the function will return the index position (starting from zero) of the first occurrence. If the "value" is not found in the list, the function will return -1.
This function loops through the elements within the arranged list, comparing each one to a designated value using the standard equality comparer for that data type. It halts the loop upon finding the first matching element and then provides the index of this occurrence. The time complexity of this function is O(n), with 'n' representing the quantity of items in the sorted list, as it involves a linear traversal of the sorted list.
Example:
Let's consider a program to demonstrate the IndexOfValue function in C#.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
SortedList<int, string> sortedList = new SortedList<int, string>();
while (true)
{
Console.WriteLine("Enter a number (or type 'done' to finish):");
string input = Console.ReadLine();
if (input.ToLower() == "done")
break;
Console.WriteLine("Enter a corresponding fruit:");
string fruit = Console.ReadLine();
int number;
if (int.TryParse(input, out number))
{
sortedList.Add(number, fruit);
}
else
{
Console.WriteLine("Invalid input. Please enter a valid number.");
}
}
int index = sortedList.IndexOfValue("Orange");
Console.WriteLine("Index of 'Orange' is: " + index);
Console.WriteLine("Items in the sorted list:");
foreach (var kvp in sortedList)
{
Console.WriteLine("Index: " + kvp.Key + ", Value: " + kvp.Value);
}
}
}
Output:
The <style> code snippet showcases a styled placeholder diagram. The diagram includes a background with a linear gradient, border radius, padding, margin, and centered text alignment. Within the diagram, there is an icon with a font size of 3rem and text with a color of #9ca3af and a font size of 1rem. This design creates a visually appealing placeholder for various web development projects.
Explanation:
The preceding code demonstrates the implementation of the IndexOfValue function within a sorted list. Initially, a SortedList is set up with an integer key and a string value. Subsequently, a continuous loop prompts the user for input, capturing both a number and the corresponding fruit name, which are then added to the sorted list. The loop will terminate upon receiving a specific user input. Following this, a variable named index is declared to store the outcome of the IndexOfValue function. The determined index is then exhibited. Subsequently, a foreach loop is employed to traverse the SortedList and print the content based on the index position.
Example 2:
Let's consider another scenario where a sorted collection employs intricate entities as its elements in C#.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
SortedList<int, Person> sortedList = new SortedList<int, Person>();
Console.WriteLine("Enter names and ages of individuals (or type 'done' to finish):");
int key = 1;
while (true)
{
Console.WriteLine("Enter name (or type 'done' to finish):");
string name = Console.ReadLine();
if (name.ToLower() == "done")
break;
Console.WriteLine("Enter age:");
int age;
if (!int.TryParse(Console.ReadLine(), out age))
{
Console.WriteLine("Invalid input. Please enter a valid age.");
continue;
}
sortedList.Add(key++, new Person(name, age));
}
int index = sortedList.IndexOfValue(new Person("", 30));
Console.WriteLine("Index of a person with age 30 is: " + index);
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
Output:
Styling for a placeholder diagram is typically defined using CSS rules. The diagram's container has a background with a linear gradient, rounded corners, padding, margin, and centered text alignment. Inside, there is an icon with a specific size and spacing, as well as text styled in a particular color and size.
Explanation:
This code snippet also showcases the utilization of the IndexOfValue method. Initially, a SortedList is established and populated with key-value pairs arranged by keys. The keys correspond to integers, while the values represent individuals' names. A while loop is implemented to acquire user input, capturing both the name and age of the individual. Subsequently, a variable "Person" is initialized to retain the index of the individual whose age corresponds to "iPersonIf" in the sortedList, returning the index if located, or -1 otherwise. Within this context, a class is defined to represent a person. Ultimately, this script exemplifies the usage of a SortedList for managing key-value pairs.
Conclusion:
In summary, the IndexOfValue function offers an effective means to locate the initial instance of a value within the SortedList. It provides the index within the key-value pair collection. This function iterates through all values in the sorted list, comparing each one to the specified value. If the value is within the SortedList, it will yield the zero-based index of the located value; alternatively, it will return -1. The time complexity associated with this function is O(n). Overall, this method will improve the capabilities of the SortedList data structure.