Skip to content

Object instantiation

When we instantiate a class, we create an object from that class. An object is basically a specific instance of the class with its own set of data.

To instantiate a class, use the new keyword, then the class name, followed by the parentheses ().

ClassName objectName = new ClassName();

Given the Person class declared in the last article as:

class Person
{
public string name;
public int age;
}

…we can instantiate an object of the Person class like this:

Person person1 = new Person();

person1 is an object of type Person.

We can access the fields of the person1 object - see fields - and assign values to them.