Define a class in C# and create two instances of the class Part 3 – Create two instances from the class
1 min read

Define a class in C# and create two instances of the class Part 3 – Create two instances from the class

Finally let’s create two instances from the class.

Inside our TestPerson class and inside the Main method instantiate the instances by using the new keyword and call the constructor you wrote with the name paramater

            var person1 = new Person("Sherlock Holmes");
            var person2 = new Person("John Watson");

Now it’s time to return the instances value name property to the console.

            Console.WriteLine(person1.Name);
            Console.WriteLine(person2.Name);

And here is the complete code.

    class TestPerson
    {
        static void Main(string[] args)
        {
            var person1 = new Person("Sherlock Holmes");
            var person2 = new Person("John Watson");

            Console.WriteLine(person1.Name);
            Console.WriteLine(person2.Name);
        }
    }

Time to run your .NET Core console app and see it in action!

Wow! Great work! Now go and write some classes!

Leave a Reply