stuart-d2
5/28/2015 - 8:58 PM

Object Initialization With List Initializers

Object Initialization With List Initializers

		
		//using System;
		//using System.Collections.Generic;  
		
		//C# program that initializes Object Lists.
		//Robot has two properties, a ID and a name.
		//These 
		
		class Robot //Used in lists .. (remember this are object Properties so it is UPPER case.)  
		{
		        public int RobotID {get;set;} 
		        public string RobotName { get; set; }
		}
		
		class Program {
		
		static void Main()
		{
		        // Initialize the list with a Collection Initializer
		        List<Robot> robotList = new List<Robot>() 
		        {
		                new Robot(){ RobotID = 1, RobotName = "BlackBot"}, 
		                new Robot() { RobotID = 2, RobotName = "WhiteBot"}
		        };
		
		
		//Init robot list 2, with new objects
		
		        List<Robot> robotList2 = new List<Robot>(); 
		        robotList2.Add(new Robot() { RobotID = 3, RobotName = "GreyBot"});
		        robotList2.Add(new Robot() { RobotID = 4, RobotName = "BlueBot"});
		        
		        Console.WriteLine(robotList.Count);
		        Console.WriteLine(robotList2.Count);
		}
		}