0% found this document useful (0 votes)
4 views

C# List Basics

C# Lists are flexible, strongly-typed collections that allow dynamic resizing and are part of the `System.Collections.Generic` namespace. Common methods include adding elements with `Add` and `AddRange`, accessing elements via index or `IndexOf`, and modifying elements with `Insert`, `Remove`, and `Clear`. Lists are ideal for dynamic collections requiring sorting and searching functionality, and best practices include specifying data types and using appropriate iteration methods.

Uploaded by

gabbhy2704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C# List Basics

C# Lists are flexible, strongly-typed collections that allow dynamic resizing and are part of the `System.Collections.Generic` namespace. Common methods include adding elements with `Add` and `AddRange`, accessing elements via index or `IndexOf`, and modifying elements with `Insert`, `Remove`, and `Clear`. Lists are ideal for dynamic collections requiring sorting and searching functionality, and best practices include specifying data types and using appropriate iteration methods.

Uploaded by

gabbhy2704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C# List Basics

can you tell me about c# lists and some common subcommands for c# lists? basically everything I
need to know as a beginner

C# Lists: A Beginner's Guide

In C#, a List is a flexible, strongly-typed collection that stores objects of the same type in an ordered
manner. Lists belong to the `System.Collections.Generic` namespace and are part of the generic
collections in C#. Lists are often preferred over arrays because they offer more flexibility (e.g., dynamic
resizing).

Declaring a List

To use a List in C#, you need to:

1. Import the `System.Collections.Generic` namespace.


2. Declare the List with the type of items it will hold.

Syntax:

csharp

using System.Collections.Generic;

List<Type> listName = new List<Type>();

Example:

csharp

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<int> numbers = new List<int>();
}
}

Common Methods (Subcommands) for C# Lists

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/5
Here are the most frequently used methods and properties:

1. Adding Elements

`Add(item)`: Adds an element to the end of the List.

csharp

numbers.Add(5); // Adds 5 to the list

`AddRange(collection)`: Adds all elements of another collection to the List.

csharp

numbers.AddRange(new int[] { 10, 15, 20 });

2. Accessing Elements

`list[index]`: Access an element by its index.

csharp

int first = numbers[0]; // Access the first element

`IndexOf(item)`: Finds the index of the first occurrence of an element.

csharp

int index = numbers.IndexOf(10);

`Contains(item)`: Checks if an element exists in the List.

csharp

bool exists = numbers.Contains(10);

3. Modifying Elements

`Insert(index, item)`: Inserts an item at a specific position.

csharp

numbers.Insert(2, 99); // Inserts 99 at index 2

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/5
`Remove(item)`: Removes the first occurrence of the item.

csharp

numbers.Remove(15);

`RemoveAt(index)`: Removes the element at a specific index.

csharp

numbers.RemoveAt(0); // Removes the first element

`Clear()`: Removes all elements from the List.

csharp

numbers.Clear();

4. Iterating Over a List

Using a `foreach` loop:

csharp

foreach (int number in numbers)


{
Console.WriteLine(number);
}

Using a `for` loop:

csharp

for (int i = 0; i < numbers.Count; i++)


{
Console.WriteLine(numbers[i]);
}

5. List Properties

`Count`: Gets the number of elements in the List.

csharp

int size = numbers.Count;

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/5
`Capacity`: Gets or sets the total number of elements the internal data structure can hold.

csharp

numbers.Capacity = 100; // Increases capacity

6. Sorting and Searching

`Sort()`: Sorts the elements in ascending order.

csharp

numbers.Sort();

`Reverse()`: Reverses the order of elements.

csharp

numbers.Reverse();

`BinarySearch(item)`: Searches for an item in a sorted List.

csharp

int position = numbers.BinarySearch(10);

7. Converting to Other Types

`ToArray()`: Converts the List to an array.

csharp

int[] array = numbers.ToArray();

`ToList()`: Converts a collection to a List.

csharp

List<int> newList = array.ToList();

Example: A Complete Program Using a List

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/5
csharp

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
// Declare and initialize a List
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };

// Add items
fruits.Add("Date");
fruits.AddRange(new string[] { "Elderberry", "Fig" });

// Access and modify items


Console.WriteLine("First fruit: " + fruits[0]);
fruits[1] = "Blueberry";

// Display all items


foreach (var fruit in fruits)
{
Console.WriteLine(fruit);
}

// Check for an item


Console.WriteLine("Contains 'Cherry'? " + fruits.Contains("Cherry"));

// Remove items
fruits.Remove("Fig");
fruits.RemoveAt(2);

// Sort and display


fruits.Sort();
Console.WriteLine("Sorted list:");
foreach (var fruit in fruits)
{
Console.WriteLine(fruit);
}
}
}

When to Use a List

When you need a dynamic collection (size can change at runtime).


When you require advanced functionality like sorting, searching, and manipulation of elements.

Best Practices

Specify the data type of the List to avoid runtime errors.


Use `foreach` for read-only iteration and `for` when modifying elements during iteration.
Be mindful of performance when working with very large Lists, especially with operations like
`Remove` (can be slow for large datasets).

If you have more specific questions, feel free to ask!

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/5

You might also like