本文整理汇总了C#中GenericList.LastIndexOf方法的典型用法代码示例。如果您正苦于以下问题:C# GenericList.LastIndexOf方法的具体用法?C# GenericList.LastIndexOf怎么用?C# GenericList.LastIndexOf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GenericList
的用法示例。
在下文中一共展示了GenericList.LastIndexOf方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
/// <summary>
/// Defines the entry point of the application.
/// </summary>
public static void Main()
{
// so that you can see all the entire printed list of results
Console.BufferHeight = 200;
var list = new GenericList<int> { 2, 3, 4, 5, 6, 7, 8, 4, 10 };
Console.WriteLine("Initial list");
Console.WriteLine(list);
list.Version();
Console.WriteLine();
// adding an element
Console.WriteLine("Adding an element");
Console.WriteLine("Adding element with value 1 to the end of the list:");
list.Add(1);
Console.WriteLine(list);
Console.WriteLine();
// accessing element by index
Console.WriteLine("Accessing element by index");
Console.WriteLine("The element at index {0} is {1}", 2, list[2]);
Console.WriteLine();
// finding element index by given value
Console.WriteLine("Finding element index by given value");
Console.WriteLine("The first index of {0} is {1}", 4, list.IndexOf(4));
Console.WriteLine("The last index of {0} is {1}", 4, list.LastIndexOf(4));
Console.WriteLine();
// removing element by index
Console.WriteLine("Removing element by index");
list.RemoveAt(1);
Console.WriteLine("Removing the element at index 1, result:");
Console.WriteLine(list);
Console.WriteLine();
// removing element by value
Console.WriteLine("Removing element by value");
list.Remove(7);
Console.WriteLine("Removing the element with value 7, result:");
Console.WriteLine(list);
Console.WriteLine();
// inserting element at given position
Console.WriteLine("Inserting element at given position");
list.InsertAt(2, 8);
Console.WriteLine("Inserted element with value 8 at index 2, result:");
Console.WriteLine(list);
Console.WriteLine();
// checking if the list contains a value
Console.WriteLine("Checking if the list contains a value");
Console.WriteLine("Checking if the list contains an element with value 2");
Console.WriteLine(list.Contains(2));
Console.WriteLine("Checking if the list contains an element with value 11");
Console.WriteLine(list.Contains(11));
Console.WriteLine();
// finding the minimal and maximal element in the GenericList<T>
Console.WriteLine("Finding the minimal and maximal element in the GenericList<T>");
Console.WriteLine("The minimal element in the GenericList is {0}", list.Min());
Console.WriteLine("The maximal element in the GenericList is {0}", list.Max());
Console.WriteLine();
// clearing the list
Console.WriteLine("Clearing the list");
Console.WriteLine("GenericList count before clearing: {0}", list.Count);
list.Clear();
Console.WriteLine("GenericList count after clearing: {0}", list.Count);
Console.WriteLine("Displaying the elements in the GenericList: {0}", list);
Console.WriteLine();
// adding new elements to the GenericList
Console.WriteLine("Adding new elements to the GenericList");
list.Add(6);
list.Add(4);
Console.WriteLine("GenericList: {0}", list);
Console.WriteLine();
System.Reflection.MemberInfo info = typeof(GenericList<>);
foreach (object attribute in info.GetCustomAttributes(false))
{
Console.WriteLine(attribute);
}
}
示例2: Main
static void Main(string[] args)
{
var list = new GenericList<int> { 12, 6, 9, 5, 45, 6, 6, 8, 95, 5 };
Console.WriteLine("GenericList :");
Console.WriteLine(list);
list.Version();
Console.WriteLine();
Console.WriteLine("Adding an element");
list.Add(1);
Console.WriteLine(list);
Console.WriteLine();
Console.WriteLine("Accessing element by index");
Console.WriteLine("The element at index {0} is {1}", 8, list[8]);
Console.WriteLine();
Console.WriteLine("The first index of {0} is {1}", 5, list.IndexOf(5));
Console.WriteLine("The last index of {0} is {1}", 5, list.LastIndexOf(5));
Console.WriteLine();
Console.WriteLine("Removing element by index");
list.RemoveAt(1);
Console.WriteLine("Removing the element at index 1, result:");
Console.WriteLine(list);
Console.WriteLine();
Console.WriteLine("Removing element by value");
list.Remove(45);
Console.WriteLine("Removing the element with value 45, result:");
Console.WriteLine(list);
Console.WriteLine();
Console.WriteLine("Inserting element at given position");
list.InsertAt(6, 14);
Console.WriteLine("Inserted element with value 14 at index 6, result:");
Console.WriteLine(list);
Console.WriteLine();
Console.WriteLine("Checking if the list contains an element with value 6");
Console.WriteLine(list.Contains(6));
Console.WriteLine();
Console.WriteLine("The min element is {0}", list.Min());
Console.WriteLine("The max element is {0}", list.Max());
Console.WriteLine();
Console.WriteLine("List Count before clearing: {0}", list.Count);
list.Clear();
Console.WriteLine("List Count after clearing: {0}", list.Count);
Console.WriteLine("Displaying the elements in the GenericList: {0}", list);
Console.WriteLine();
Console.WriteLine("Adding new elements to the GenericList");
list.Add(6);
list.Add(5);
list.Add(89);
list.Add(12);
list.Add(5);
list.Add(99);
list.Add(65);
list.Add(456);
list.Add(44);
list.Add(8768);
list.Add(45488);
list.Add(11);
Console.WriteLine("GenericList: {0}", list);
Console.WriteLine();
System.Reflection.MemberInfo info = typeof(GenericList<>);
foreach (object attribute in info.GetCustomAttributes(false))
{
Console.WriteLine(attribute);
}
}
示例3: TestGenericListMethods
/// <summary>
/// Tests the generic list methods.
/// </summary>
public void TestGenericListMethods()
{
var list = new GenericList<int> { 2, 3, 4, 5, 6, 7, 8, 4, 10 };
Console.WriteLine("Initial list");
Console.WriteLine(list);
list.Version();
Console.WriteLine();
// adding an element
Console.WriteLine("Adding an element");
Console.WriteLine("Adding element with value 1 to the end of the list:");
list.Add(1);
Console.WriteLine(list);
Console.WriteLine();
// accessing element by index
Console.WriteLine("Accessing element by index");
Console.WriteLine("The element at index {0} is {1}", 2, list[2]);
Console.WriteLine();
// finding element index by given value
Console.WriteLine("Finding element index by given value");
Console.WriteLine("The first index of {0} is {1}", 4, list.IndexOf(4));
Console.WriteLine("The last index of {0} is {1}", 4, list.LastIndexOf(4));
Console.WriteLine();
// removing element by index
Console.WriteLine("Removing element by index");
list.RemoveAt(1);
Console.WriteLine("Removing the element at index 1, result:");
Console.WriteLine(list);
Console.WriteLine();
// removing element by value
Console.WriteLine("Removing element by value");
list.Remove(7);
Console.WriteLine("Removing the element with value 7, result:");
Console.WriteLine(list);
Console.WriteLine();
// inserting element at given position
Console.WriteLine("Inserting element at given position");
list.InsertAt(2, 8);
Console.WriteLine("Inserted element with value 8 at index 2, result:");
Console.WriteLine(list);
Console.WriteLine();
// checking if the list contains a value
Console.WriteLine("Checking if the list contains a value");
Console.WriteLine("Checking if the list contains an element with value 2");
Console.WriteLine(list.Contains(2));
Console.WriteLine("Checking if the list contains an element with value 11");
Console.WriteLine(list.Contains(11));
Console.WriteLine();
// finding the minimal and maximal element in the GenericList<T>
Console.WriteLine("Finding the minimal and maximal element in the GenericList<T>");
Console.WriteLine("The minimal element in the GenericList is {0}", list.Min());
Console.WriteLine("The maximal element in the GenericList is {0}", list.Max());
Console.WriteLine();
// clearing the list
Console.WriteLine("Clearing the list");
Console.WriteLine("GenericList count before clearing: {0}", list.Count);
list.Clear();
Console.WriteLine("GenericList count after clearing: {0}", list.Count);
Console.WriteLine("Displaying the elements in the GenericList: {0}", list);
Console.WriteLine();
// adding new elements to the GenericList
Console.WriteLine("Adding new elements to the GenericList");
list.Add(6);
list.Add(4);
Console.WriteLine("GenericList: {0}", list);
Console.WriteLine();
}