SoFunction
Updated on 2025-04-14

C# Implementation of converting student lists into dictionary

When developing applications, managing and processing data structures is a very important part. In this blog post, we will explore how to convert a student list into a dictionary with the student’s name as the key and the student’s index in the list as the value. This transformation is very practical in many scenarios, especially when a quick lookup or index is required.

Background knowledge

In C#, we can useList<T>To store the student object and then convert it toDictionary<TKey, TValue>. The dictionary provides efficient search capabilities, allowing us to obtain values ​​in constant time.

Sample code

Here is a sample code for converting a student list to a dictionary:

using System;
using ;
using ;

class Student
{
    public string Name { get; set; }
    
    public Student(string name)
    {
        Name = name;
    }
}

class Program
{
    static void Main()
    {
        // Create a student list        List&lt;Student&gt; students = new List&lt;Student&gt;
        {
            new Student("Alice"),
            new Student("Bob"),
            new Student("Charlie"),
            new Student("David"),
            new Student("Eva")
        };

        // Convert student list to dictionary        Dictionary&lt;string, int&gt; studentDictionary = students
            .Select((student, index) =&gt; new { , Index = index })
            .ToDictionary(x =&gt; , x =&gt; );

        // Print dictionary content        foreach (var kvp in studentDictionary)
        {
            ($"Name: {}, Index: {}");
        }
    }
}

Code parsing

Define student classes
Let's first define aStudentClass, contains oneNameAttributes, representing the student's name.

class Student
{
    public string Name { get; set; }
    public Student(string name)
    {
        Name = name;
    }
}

Create a student list
Let's create aList<Student>To store multiple student objects.

List<Student> students = new List<Student>
{
    new Student("Alice"),
    new Student("Bob"),
    new Student("Charlie"),
    new Student("David"),
    new Student("Eva")
};

Convert to dictionary
We use LINQSelectMethods to traverse the student list and encapsulate each student's name with its index into an anonymous object. Next, useToDictionaryMethod converts it to a dictionary.

Dictionary<string, int> studentDictionary = students
    .Select((student, index) => new { , Index = index })
    .ToDictionary(x => , x => );

Output dictionary content
Finally, we loop through the dictionary and print each student’s name and its index in the list.

foreach (var kvp in studentDictionary)
{
    ($"Name: {}, Index: {}");
}

Running results

After running the above code, the output will look like this:

Name: Alice, Index: 0
Name: Bob, Index: 1
Name: Charlie, Index: 2
Name: David, Index: 3
Name: Eva, Index: 4

in conclusion

Through the above example, we successfully convert the student list into a dictionary with name as key and index as value. This structure not only improves search efficiency, but also simplifies data management. In practical applications, this method can be widely used in various scenarios that require quick access and retrieval of data.

This is the article about the implementation of converting student lists into dictionary here. For more related content to convert C# student lists into dictionary, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!