|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListTUsage
{
class Program
{
static void Main(string[] args)
{
// Create a new list and add some numbers to it.
List<int> list = new List<int>();
list.Add(4);
list.Add(1);
list.Add(44);
list.Add(3);
list.Add(5);
list.Add(20);
list.Add(10);
Console.WriteLine("All Integers in the list.");
Console.WriteLine(GetNumbersAsString(list));
Console.WriteLine();
Console.WriteLine("Add a 3 if it is not already in the list and show all Integers again.");
// Contains will return a Boolean saying if the provided Object is already in the list.
if (!list.Contains(3))
{
Console.WriteLine("A 3 was added to the list.");
list.Add(3);
}
else
{
Console.WriteLine("A 3 was not added to the list.");
}
Console.WriteLine(GetNumbersAsString(list));
Console.WriteLine();
Console.WriteLine("Add a 3 and show all Integers again.");
// Doubles are allowed.
list.Add(3);
Console.WriteLine(GetNumbersAsString(list));
Console.WriteLine();
Console.WriteLine("Sort the list and show all Integers again.");
// Sort the list.
list.Sort();
Console.WriteLine(GetNumbersAsString(list));
Console.WriteLine();
Console.WriteLine("Reverse the list and show all Integers again.");
// Reverse the list.
list.Reverse();
Console.WriteLine(GetNumbersAsString(list));
Console.WriteLine();
Console.WriteLine("Remove a 4 from the list and show all Integers again.");
// Remove a 4 from the list. If it succeeds this Function returns True.
if (list.Remove(4))
{
Console.WriteLine("A 4 was successfully removed from the list.");
}
else
{
Console.WriteLine("A 4 could not be removed from the list.");
}
Console.WriteLine(GetNumbersAsString(list));
Console.WriteLine();
Console.WriteLine("Remove another 4 from the list and show all Integers again.");
// Remove a 4 from the list. If it succeeds this Function returns True.
if (list.Remove(4))
{
Console.WriteLine("Another 4 was successfully removed from the list.");
}
else
{
Console.WriteLine("Another 4 could not be removed from the list.");
}
Console.WriteLine(GetNumbersAsString(list));
Console.WriteLine();
Console.WriteLine("Create a new list and give it all items from the old list.");
// The constructor has two overloads, one that accepts an Integer that will set the
// initial size of the list (List<T> still uses an Array internally).
// The other one accepts another collection as argument.
// This will create a new list containing all of the elements from the passed list.
List<int> list2 = new List<int>(list);
Console.WriteLine(GetNumbersAsString(list2));
Console.WriteLine();
Console.WriteLine("This concludes the brief introduction to the List<T> Class.");
Console.ReadKey();
}
private static string GetNumbersAsString(List<int> list)
{
string s = string.Empty;
foreach (int i in list)
{
s += i + ", ";
}
// Delete the last comma.
return s.Substring(0, s.Length - 2);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Sander Rossel is a Microsoft certified professional developer with experience and expertise in .NET and .NET Core (C#, ASP.NET, and Entity Framework), SQL Server, Azure, Azure DevOps, JavaScript, MongoDB, and other technologies.
He is the owner of
JUUN Software, a company specializing in custom software. JUUN Software uses modern, but proven technologies, such as .NET Core, Azure and Azure DevOps.
You can't miss
his books on Amazon and
his free e-books on Syncfusion!
He wrote a JavaScript LINQ library,
arrgh.js (works in IE8+, Edge, Firefox, Chrome, and probably everything else).
Check out his
prize-winning articles on CodeProject as well!