- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Searching the index of specified object in Collection in C#
To search the index of specified object in Collection, the code is as follows −
Example
using System; using System.Collections.Specialized; public class Demo { public static void Main() { StringCollection strCol = new StringCollection(); strCol.Add("Accessories"); strCol.Add("Books"); strCol.Add("Electronics"); strCol.Add("Books"); Console.WriteLine("StringCollection elements..."); foreach (string res in strCol) { Console.WriteLine(res); } strCol.Insert(2, "Headphone"); Console.WriteLine("StringCollection elements...UPDATED"); foreach (string res in strCol) { Console.WriteLine(res); } Console.WriteLine("Index of specific object Electronics? = "+strCol.IndexOf("Electronics")); } }
Output
This will produce the following output −
StringCollection elements... Accessories Books Electronics Books StringCollection elements...UPDATED Accessories Books Headphone Electronics Books Index of specific object Electronics? = 3
Example
Let us now see another example −
using System; using System.Collections.Specialized; public class Demo { public static void Main() { StringCollection stringCol = new StringCollection(); String[] arr = new String[] { "100", "200", "300", "400", "500" }; Console.WriteLine("Array elements..."); foreach (string res in arr) { Console.WriteLine(res); } stringCol.AddRange(arr); Console.WriteLine("Does the specified string is in the StringCollection? = "+stringCol.Contains("800")); Console.WriteLine("Total number of elements = "+stringCol.Count); Console.WriteLine("Iterating through StringCollection = "+stringCol.Count); StringEnumerator myenum = stringCol.GetEnumerator(); while (myenum.MoveNext()) Console.WriteLine(myenum.Current); Console.WriteLine("Index of specific object 500? = "+stringCol.IndexOf("500")); Console.WriteLine("Index of specific object 1000? = "+stringCol.IndexOf("1000")); } }
Output
This will produce the following output −
Array elements... 100 200 300 400 500 Does the specified string is in the StringCollection? = False Total number of elements = 5 Iterating through StringCollection = 5 100 200 300 400 500 Index of specific object 500? = 4 Index of specific object 1000? = -1
- Related Articles
- Remove element at specified index of Collection in C#
- Get or set the element at specified index in Collection in C#
- Insert an element into Collection at specified index in C#
- Getting index of the specified value in a SortedList object in C#
- Getting the index of the specified key in a SortedList object in C#
- Getting the key at the specified index of a SortedList object in C#
- Getting the value at the specified index of a SortedList object in C#
- How to insert the elements of a collection into the List at the specified index in C#?
- Check if a SortedSet object is a proper subset of the specified collection in C#
- Check if a SortedSet object is a proper superset of the specified collection in C#
- Insert all elements of other Collection to Specified Index of Java ArrayList
- Remove from the specified index of the StringCollection in C#
- Remove from the specified index of a SortedList in C#
- Copy StringCollection at the specified index of array in C#
- Insert at the specified index in StringCollection in C#

Advertisements