Link to home
Start Free TrialLog in
Avatar of ASPDEV
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.

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; }
}

Open in new window


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
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ASPDEV
ASPDEV

ASKER

Thanks.