C# .NET - purpose of using new keyword in this code

Asked By anbu n on 13-May-13 10:49 PM
using System;
using System.Collections.Generic;

class Test // Used in Lists.
{
    public int A { get; set; }
    public string B { get; set; }
}

class Program
{
    static void Main()
    {
      // Initialize list with collection initializer.
      List<Test> list1 = new List<Test>()
    {
      new Test(){ A = 1, B = "Jessica"},
      new Test(){ A = 2, B = "Mandy"}
    };

      // Initialize list with new objects.
      List<Test> list2 = new List<Test>();
      list2.Add(new Test() { A = 3, B = "Sarah" });
      list2.Add(new Test() { A = 4, B = "Melanie" });

      // Write number of elements in the lists.
      Console.WriteLine(list1.Count);
      Console.WriteLine(list2.Count);
    }
}
----------------------------------------------------------------
in the above code , what does new keyword do/ what's its purpose / use
Sandeep Mittal replied to anbu n on 14-May-13 12:38 AM
Here, new keyword is use to initialize the objects and allocate memory for the same.
In the code
List<Test> list1 = new List<Test>()
{
   new Test(){ A = 1, B = "Jessica"},
   new Test(){ A = 2, B = "Mandy"}
};

We have defined collection of Test class.
Inside {} braces, we are initializing class objects and allocating the memory with the code
new Test(){ A = 1, B = "Jessica"}
Then with the code
List<Test> list1 = new List<Test>()
we are initializing the collection of Test Class and allocating the memory for the same.
Robbe Morris replied to anbu n on 14-May-13 08:27 AM
"new" creates a new "instance" of a class type.

 var car1 = new Car();
 var car2 = new Car();
 var car3 = new Car();

 I know have three "instances" of the same type of car.  The only question is which one do I drive to the liquor store?
Jitendra Faye replied to anbu n on 22-May-13 09:41 AM
The purpose of Using new keyword is that to allocate the memory for that particular type.

example-

List<Test> list1 ;
 doesn't create memory for list1 variable

but

list1= new List<Test>();

will allocate memory.