Skip to content

Instantly share code, notes, and snippets.

@Lahirutech
Last active May 26, 2021 14:06
Show Gist options
  • Save Lahirutech/7cade7e17ab5ed1a1a15a877680179c2 to your computer and use it in GitHub Desktop.
Save Lahirutech/7cade7e17ab5ed1a1a15a877680179c2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// adding elements using add() method
var Idnumbers = new List<int>();
Idnumbers.Add(1);
Idnumbers.Add(3);
Idnumbers.Add(5);
Idnumbers.Add(7);
Console.WriteLine("No of elelemts numbers: "+ Idnumbers.Count);
var Mycities = new List<string>();
Mycities.Add("New York");
Mycities.Add("Sri Lanka");
Mycities.Add("Mumbai");
Mycities.Add("Chicago");
Mycities.Add(null); // null is allowed
Console.WriteLine("No of elelemts MySities: " + Mycities.Count);
// adding elements using collection initializer syntax
var bigCities = new List<string>() {"New York", "London", "Mumbai", "Chicago"};
Console.WriteLine("No of elelemts bigCities: " + bigCities.Count);
var students = new List<StudentDetails>() {
new StudentDetails(){ Id = 1, Name="Bill"},
new StudentDetails(){ Id = 2, Name="Steve"},
new StudentDetails(){ Id = 3, Name="Ram"},
new StudentDetails(){ Id = 4, Name="Abdul"}
};
Console.WriteLine("No of elelemts Students: " + students.Count);
}
}
class StudentDetails{
public int Id { get; set; }
public string Name { get; set; }
}
/*
No of elelemts numbers: 4
No of elelemts MySities: 5
No of elelemts bigCities: 4
No of elelemts Students: 4
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment