Skip to content

Instantly share code, notes, and snippets.

@asvignesh
Created October 11, 2021 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asvignesh/a37854da8e9e5ffbfe9775853b2f7bfb to your computer and use it in GitHub Desktop.
Save asvignesh/a37854da8e9e5ffbfe9775853b2f7bfb to your computer and use it in GitHub Desktop.
C# LIST
Creating 3 Students and add to List
Print all 3 Students
1, Ramya, CSE
2, Devi, CSE
3, Priya, IT
Find department of a student name Ramya
CSE
Remove 2nd student from list
Print all 2 Students
1, Ramya, CSE
2, Devi, CSE
using System;
using System.Collections.Generic;
using System.Linq;
namespace ListSample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Creating 3 Students and add to List");
List<StudentDto> students = new List<StudentDto>();
StudentDto student1 = new StudentDto
{
id = 1,
name = "Ramya",
department = "CSE"
};
students.Add(student1);
StudentDto student2 = new StudentDto
{
id = 2,
name = "Devi",
department = "CSE"
};
students.Add(student2);
StudentDto student3 = new StudentDto
{
id = 3,
name = "Priya",
department = "IT"
};
students.Add(student3);
Console.WriteLine("Print all 3 Students");
foreach (var student in students)
Console.WriteLine(student.id + ", " + student.name + ", " + student.department);
Console.WriteLine("Find department of a student name Ramya");
var result = from s in students
where s.name == "Ramya"
select s.department;
Console.WriteLine(result.First());
Console.WriteLine("Remove 2nd student from list");
students.RemoveAt(2);
Console.WriteLine("Print all 2 Students");
foreach (var student in students)
Console.WriteLine(student.id + ", " + student.name + ", " + student.department);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListSample
{
class StudentDto
{
public int id { get; set; }
public string name { get; set; }
public string department { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment