ASPDEV
asked on
C# List Assigned Values
Hello Experts,
I have an issue with C# List, when I add values it showing the duplicates data.
Output:
2
2
Doe
The count was correct, but I was expecting below output and the list shows duplicates:
2 -- >Count
1 --> ID
John -- > Name
Please help, what I was doing incorrect.
Thanks,
ASPDEV
I have an issue with C# List, when I add values it showing the duplicates data.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
Student std = new Student();
List<Student> studentList = new List<Student>();
std.ID = 1;
std.Name = "John";
studentList.Add(std);
std.ID = 2;
std.Name = "Doe";
studentList.Add(std);
Console.WriteLine(studentList.Count);//count
Console.WriteLine(studentList[0].ID); //ID
Console.WriteLine(studentList[0].Name); //Name
}
}
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
}
Output:
2
2
Doe
The count was correct, but I was expecting below output and the list shows duplicates:
2 -- >Count
1 --> ID
John -- > Name
Please help, what I was doing incorrect.
Thanks,
ASPDEV
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER