untung99play.xyz: C List Tutorial Everything You Need To Learn About List In C
Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.
Berikut adalah artikel atau berita tentang Harian untung99play.xyz dengan judul untung99play.xyz: C List Tutorial Everything You Need To Learn About List In C yang telah tayang di untung99play.xyz terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di koresponden@untung99play.xyz, Terimakasih.
C# List class represents a collection of strongly typed objects that can be accessed by index. This tutorial teaches how to work with lists in C# using the C# List class to add, find, sort, reverse, and search items in a collection of objects using List class methods and properties.
What is C# List?
List
Create a List in C#
C# List is a generic class and is defined in the System.Collections.Generic namespace. You must import this namespace in your project to access the List
using System.Collections.Generic;
List
The code snippet in Listing 1 creates a List of Int16 and a list of string types. The last part of the code creates a List
// List with default capacity
List list = new List();
// List with capacity = 5
List authors = new List(5);
string[] animals = { "Cow", "Camel", "Elephant" };
List animalsList = new List(animals);
Listing 1.
As you can see from Listing 1, the List
Add an item to a C# List
The Add method adds an element to a C# List. For example, the code snippet in Listing 2 creates two List
// Dynamic ArrayList with no size limit
List numberList = new List();
numberList.Add(32);
numberList.Add(21);
numberList.Add(45);
numberList.Add(11);
numberList.Add(89);
// List of string
List authors = new List(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
Listing 2.
We can also add a collection to a List. The AddRange method is used to add a group to a List. For example, the code snippet in Listing 3 adds an array of strings to a List.
// Collection of string
string[] animals = { "Cow", "Camel", "Elephant" };
// Create a List and add a collection
List animalsList = new List();
animalsList.AddRange(animals);
foreach (string a in animalsList)
Console.WriteLine(a);
Listing 3.
Loop through a C# List items
The C# List is a collection of items. We can use a foreach loop to loop through its items. The code snippet in Listing 6 reads all list items and displays them on the console.
foreach (string a in authors)
Console.WriteLine(a);
Listing 4.
We can use the collection’s index to retrieve an item in a C# List at a specific position. For example, the following code snippet reads the 3rd item in the List.
Console.WriteLine(authors[2]);
C# List properties
List class properties include the following:
- Capacity – Number of elements List
can contain. The default capacity of a List is 0. - Count – Number of elements in List
.
The code snippet in Listing 5 gets the value of these properties.
ArrayList authorsArray = new ArrayList();
authorsArray.Add("Mahesh Chand");
authorsArray.Add("Praveen Kumar");
authorsArray.Add("Raj Kumar");
authorsArray.Add("Dinesh Beniwal");
authorsArray.Add("David McCarter");
Console.WriteLine("Count: " + authors.Count);
Console.WriteLine("Capacity: " + authors.Capacity);
Listing 5.
Insert an item at a position in a C# List
The Insert method of the List class inserts an object at a given position. The first parameter of the method is the 0th-based index in the List.
The InsertRange method can insert a collection at the given position.
The code snippet in Listing six inserts a string at the 3rd position and an array at the 2nd position of the List
authors.Insert(3, "Bill Author");
// Collection of new authors
string[] newAuthors = { "New Author1", "New Author2", "New Author3" };
// Insert array at position 2
authors.InsertRange(2, newAuthors);
Listing 6.
Remove an item from a C# List
The List class provides Remove methods that can be used to remove an item or a range of items.
The Remove method removes the first occurrence of the given item in the List. For example, the following code snippet removes the first occurrence of ‘New Author1’.
// Remove an item
authors.Remove("New Author1");
The RemoveAt method removes an item at the given position. For example, the following code snippet removes the item at the 3rd position.
// Remove 3rd item
authors.RemoveAt(3);
The RemoveRange method removes a list of items from the starting index to the number of items. For example, the following code snippet removes two items starting at the 3rd position.
// Remove a range
authors.RemoveRange(3, 2);
The Clear method removes all items from a List
// Remove all items
authors.Clear();
Clear all items from a C# List
The Clear method removes all items from a C# List. For example, the following code snippet removes all items from a List.
// Remove all items
authors.Clear();
Check if an item exists in a C# List
The IndexOf method finds an item in a List. The IndexOf method returns -1 if no items are located in the List.
The following code snippet finds a string and returns the matched position of the item.
int idx = authors.IndexOf("Naveen Sharma");
if (idx > 0)
Console.WriteLine($"Item index in List is: {idx}");
else
Console.WriteLine("Item not found");
We can also specify the position in a List from which the IndexOf method can start searching.
For example, the following code snippet finds a string starting at the 3rd position in a String.
Console.WriteLine(authors.IndexOf("Naveen Sharma", 2));
The LastIndexOf method finds an item from the end of the List.
The following code snippet looks for a string in the backward direction and returns the index of the item if found.
Console.WriteLine(authors.LastIndexOf("Mahesh Chand"));
The complete example is listed in Listing 7.
// List of string
List authors = new List(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
int idx = authors.IndexOf("Naveen Sharma");
if (idx > 0)
Console.WriteLine($"Item index in List is: {idx}");
else
Console.WriteLine("Item not found");
Console.WriteLine(authors.IndexOf("Naveen Sharma", 2));
Console.WriteLine(authors.LastIndexOf("Mahesh Chand"));
Listing 7.
Sort a C# List
The Sort method of List
The following code example in Listing eight sorts List items and displays both the original and sorted order of the List items.
// List of string
List authors = new List(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
Console.WriteLine("Original List items");
Console.WriteLine("===============");
// Print original order
foreach (string a in authors)
Console.WriteLine(a);
// Sort list items
authors.Sort();
Console.WriteLine();
Console.WriteLine("Sorted List items");
Console.WriteLine("===============");
// Print sorted items
foreach (string a in authors)
Console.WriteLine(a);
Listing 8.
The output of Listing eight looks like Figure 2.
Figure 2.
Reverse a C# List
The Reverse method of List
The following code snippet reverses a List.
// List of string
List authors = new List(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
Console.WriteLine("Original List items");
Console.WriteLine("===============");
// Print original order
foreach (string a in authors)
Console.WriteLine(a);
// Reverse list items
authors.Reverse();
Console.WriteLine();
Console.WriteLine("Sorted List items");
Console.WriteLine("===============");
// Print reversed items
foreach (string a in authors)
Console.WriteLine(a);
Listing 9.
The output of Listing nine looks like Figure 3.
Figure 3.
Find an Item in a C# List
The BinarySearch method of List
The following code snippet returns an index of a string in a List.
int bs = authors.BinarySearch("Mahesh Chand");
Import items to a C# List from another List
You can use the AddRange method of List to import items from one List into another list. But make sure the item types are the same in both lists. For example, the following code snippet creates two List objects and copies all items of listTwo into listOne.
// Program: Copy items from one list to another list
Console.WriteLine("Import one list to another!");
// Create List1
List listOne = new();
listOne.Add("One");
listOne.Add("Two");
listOne.Add("Three");
listOne.Add("Four");
listOne.Add("Five");
// Create List2
List listTwo = new();
listTwo.Add("A");
listTwo.Add("B");
listTwo.Add("C");
// Add List2 to List1
listOne.AddRange(listTwo);
// Display
foreach(string item in listOne)
Console.WriteLine(item);
Console.ReadKey();
Convert a C# List to an array
You can use the ToArray() method of the C# List class to convert a list into an array.
int[] a = number.ToArray();
Join two C# Lists
You can use the AddRange method to merge a C# List with an existing C# List. Here is a detailed article on How to Merge Two C# Lists.
List1.AddRange(List2);
Summary
This article demonstrated how to use a List