C# list of Custom Class object
In the previous article, we learned about C# list in detail now this article is an extended version of the previous article if you don't read the previous article you can click here to visit this in previous will implement C# list with int data type but for this, we will create our custom class and make a list of this class object and use list methods over there.
How to create a custom class for use in the C# list?
Now we are going to create a student class that holds information about a student for this example we take student roll no, Name, and TotalMarks are storing we can store as many parameters we want to store but making this simpler and easy to understand we are taking only 3 parameters you can see all the perimeter must be public because we are going to access these perimeters out of the class and then we need to create a class constructer that will take value from the user and assign it to class variables.
class student
{
public int rollNo;
public string Name;
public int TotalMarks;
public student(int rollNo, string Name, int TotalMarks)
{
this.rollNo = rollNo;
this.Name = Name;
this.TotalMarks = TotalMarks;
}
}
A complete program of the list of C# with custom class and custom method.
In this example, I am going to create a program that will have the capability to add the student in the c# list as well as getting the name and marks of a student by student id and can delete the student from the id or from the index.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algomentor
{
class student
{
public int rollNo;
public string Name;
public int TotalMarks;
public student(int rollNo, string Name, int TotalMarks)
{
this.rollNo = rollNo;
this.Name = Name;
this.TotalMarks = TotalMarks;
}
}
class Program
{
public static void DisplayStudent(List<student> student)
{
foreach(var st in student)
{
Console.WriteLine("Roll No " + st.rollNo);
Console.WriteLine("Name " + st.Name);
Console.WriteLine("Total Marks " + st.TotalMarks);
Console.WriteLine("--------------------------------");
}
}
public static void GetNameById(int id,List<student> student)
{
foreach(var st in student)
{
if(st.rollNo == id)
{
Console.WriteLine("--------------------------------");
Console.WriteLine("Name " + st.Name);
Console.WriteLine("--------------------------------");
}
}
}
public static void GetNameAndMarksById(int id, List<student> student)
{
foreach (var st in student)
{
if (st.rollNo == id)
{
Console.WriteLine("--------------------------------");
Console.WriteLine("Name " + st.Name);
Console.WriteLine("Total Marks " + st.TotalMarks);
Console.WriteLine("--------------------------------");
}
}
}
public static void DeleteStudentById(int id,List<student> st)
{
int loc = -1;
for(int i=0;i<st.Count;i++)
{
if(id==st[i].rollNo)
{
loc = i;
}
}
if(loc == -1)
{
Console.WriteLine("--------------------------------");
Console.WriteLine("Element dose not exist");
Console.WriteLine("--------------------------------");
}
else
{
st.RemoveAt(loc);
Console.WriteLine("--------------------------------");
Console.WriteLine("Element deleted successfully");
Console.WriteLine("--------------------------------");
}
}
static void Main(string[] args)
{
// created the list of student
List<student> students = new List<student>();
//Adding some student into it
students.Add(new student(11, "Algo", 200));
students.Add(new student(21, "Rohit", 220));
students.Add(new student(31, "preeti", 210));
students.Add(new student(41, "Jack", 215));
students.Add(new student(51, "Rayan", 190));
//displaying student
DisplayStudent(students);
//Getting student name by his id
GetNameById(21, students);
//Getting name and marks by id
GetNameAndMarksById(31, students);
//Removing student by Index
students.RemoveAt(1); // it will remove the student whose index is 1
// Removing student by id
DeleteStudentById(41, students);
//displaying student
DisplayStudent(students);
Console.Read();
}
}
}
with the concept, you can create as many functions as you can create for a list of C# the concept will remain the same that why I do only for some methods but you can deep dive in the complexity to explore the list in c#.
Another article you can explore on our website
With all that concept I will conclude that article and thank you so much for Reading that full article we hope you enjoy the article and learn something from that.
0 Comments