.NET

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: