Create a list of students

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Collections.Generic;


class Student
{
    public string Name { get; set; }
	
    public Student(string name)
    {
        this.Name = name;
    }
}



public class Program
{
	public static void Example1()
	{
		var students = new List<Student>();
		students.Add(new Student("John"));
		students.Add(new Student("Dick"));
		students.Add(new Student("Harry"));
		
		foreach (var s in students)
		{
			Console.WriteLine(s.Name);
		}
	}
	
	public static void Example2()
	{
		//This does the thing but with a more advanced syntax
		var students = new List<Student>
		{
		    new Student("John"),
		    new Student("Dick"),
		    new Student("Harry")
		};
		
		foreach (var s in students)
		{
			Console.WriteLine(s.Name);
		}
	}
	
	public static void Main()
	{
		Example1();
		Example2();
	}
	
	
}
;