StringCollection
string[] names = new string[]{"Mahesh Chand", "Mike Gold", "Praveen Kumar", "Raj Beniwal"};
foreach (string name in authorNames)
{
Console.WriteLine(name);
}
Console.WriteLine("Position of Mike Gold is " + authorLocation.ToString());
{
Console.WriteLine("Mike Gold is at position: " + authorNames.IndexOf("Mike Gold"));
}
foreach (string name in newAuthorList)
{
Console.WriteLine(name);
}
int authorLocation = authorNames.IndexOf("Mike Gold");
string authorName = authorNames[authorLocation];
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
namespace StringCollectionSample
{
class Program
{
static void Main(string[] args)
{
// Create a StringCollection object
StringCollection authorNames = new StringCollection();
// Add string using Add method
authorNames.Add("Mahesh Chand");
authorNames.Add("Mike Gold");
authorNames.Add("Praveen Kumar");
authorNames.Add("Raj Beniwal");
// Add an array of string using AddRange
string[] names = new string[]{"Mahesh Chand", "Mike Gold", "Praveen Kumar", "Raj Beniwal"};
authorNames.AddRange(names);
// Insert an string at a specified index
authorNames.Insert(5, "New Author");
// authorNames.Clear();
// authorNames.Remove("Mike Gold");
// authorNames.RemoveAt(5);
if (authorNames.Contains("Mike Gold"))
{
Console.WriteLine("Mike Gold is at position: " + authorNames.IndexOf("Mike Gold"));
}
int authorLocation = authorNames.IndexOf("Mike Gold");
string authorName = authorNames[authorLocation];
Console.WriteLine("Position of Mike Gold is " + authorLocation.ToString());
Console.WriteLine("Total items in string collection: " + authorNames.Count.ToString());
foreach (string name in authorNames)
{
Console.WriteLine(name);
}
// Copy Collection to new Array
string[] newAuthorList = new string[authorNames.Count];
authorNames.CopyTo(newAuthorList, 0);
foreach (string name in newAuthorList)
{
Console.WriteLine(name);
}
//int[] intArray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
//foreach (int i in intArray)
//{
// System.Console.WriteLine(i);
//}
Console.ReadLine();
}
}
}