Hello here my Question - I am an absolute beginner of C#
When I started a little programming there was no such thing as Objects
I have following situation. I have a Excel list similar like this
| Students | German | English | Maths | ... |
| Student 1 | 3 | 2 | 1 | ... |
| Student 2 ... | 4 | 1 | 5 | ... |
I want to make a list of the hole table to make some calculations with it.
My problem is, I know how to make a list. But this list is with more dimensions.
On my search through the I-Net I found lists with tuples, with dictionaries, with a classes :-( ...
I don´t want to use an array, because its not flexible enough.
I don´t know what kind of list I need, how to read/write from it, delete subjects...
It would be great to see your explanations on an example.
Thanks for your help

Naimish MakwanaRank #324 · 7.7K reputationPosted Sep 5, 2024, 5:04 AM EDT
Certainly! There are several approaches you can take to handle multidimensional data in C#. Here are a couple of alternatives that might suit your needs:
1. Using a List of Dictionaries
If you prefer not to use classes, you can use a list of dictionaries. Each dictionary can represent a student and their subjects.
List> students = new List>(); var student1 = new Dictionary { { "Name", "Student 1" }, { "German", 3 }, { "English", 2 }, { "Maths", 1 } }; var student2 = new Dictionary { { "Name", "Student 2" }, { "German", 4 }, { "English", 1 }, { "Maths", 5 } }; students.Add(student1); students.Add(student2);2. Using a DataTable
A
DataTableis another flexible option, especially if you’re dealing with tabular data like an Excel sheet.DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("German", typeof(int)); table.Columns.Add("English", typeof(int)); table.Columns.Add("Maths", typeof(int)); table.Rows.Add("Student 1", 3, 2, 1); table.Rows.Add("Student 2", 4, 1, 5);3. Using a List of Tuples
Tuples can be a simple way to store data without creating a full class.
List students = new List { ("Student 1", 3, 2, 1), ("Student 2", 4, 1, 5) };Comparison of Approaches
Example: Using DataTable
Here’s how you can manipulate data in a
DataTable:// Adding a new row table.Rows.Add("Student 3", 5, 3, 4); // Updating a value table.Rows[0]["Maths"] = 2; // Deleting a row table.Rows[1].Delete();Each approach has its pros and cons, so choose the one that best fits your specific needs and the complexity of your data operations.
Thanks
Hanif HefazRank #883 · 2.4K reputationPosted Sep 2, 2024, 4:36 PM EDT
Welcome to the world of C#. Looks like you're looking to manage a list of students and their grades in different subjects. Given your requirements, using a class to represent each student and their grades would be a great approach. This way, you can easily add, remove, and modify grades for each student.
Here is how to do it in case if you don't know:
public class Student { public string Name { get; set; } public Dictionary Grades { get; set; } public Student(string name) { Name = name; Grades = new Dictionary(); } public void AddGrade(string subject, int grade) { Grades[subject] = grade; // Overwrites if the subject already exist } public void RemoveGrade(string subject) { Grades.Remove(subject); } public int GetGrade(string subject) { return Grades.TryGetValue(subject, out int grade) ? grade : -1; // Return -1 if subject not found } }using System; using System.Collections.Generic; class Program { static void Main() { List students = new List(); // Adding students Student student1 = new Student("Student 1"); student1.AddGrade("German", 3); student1.AddGrade("English", 2); student1.AddGrade("Maths", 1); students.Add(student1); Student student2 = new Student("Student 2"); student2.AddGrade("German", 4); student2.AddGrade("English", 1); student2.AddGrade("Maths", 5); students.Add(student2); // Displaying grades foreach (var student in students) { Console.WriteLine($"Grades for {student.Name}:"); foreach (var subject in student.Grades) { Console.WriteLine($"{subject.Key}: {subject.Value}"); } Console.WriteLine(); } // Removing a subject student1.RemoveGrade("Maths"); Console.WriteLine($"After removing Maths, grades for {student1.Name}:"); foreach (var subject in student1.Grades) { Console.WriteLine($"{subject.Key}: {subject.Value}"); } } }Main Program:
Using a class with a dictionary gives you flexibility to handle different subjects and their related grades dynamically. You can easily add or remove subjects and grades as needed. This approach will help you manage your data more effectively as you continue to learn C#. Happy coding!