C# program to create an array of objects

How to create an array of objects in C#:

In this post, we will learn how to create an array of objects in C#. We will create a class and an array. This array will hold different objects of the class. We will also learn how to access any of the objects in this array.

Class and objects:

A class is a skeleton or it defines the structure. You can also think it like a blueprint. We can create different objects with different properties for a single class.

Let’s take a look at the below program:

using System;

namespace c_sharp
{
    class Message
    {
        string Data;
        public Message(string Msg)
        {
            this.Data = Msg;
        }

        public void PrintMsg()
        {
            Console.WriteLine(this.Data);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Message Msg1 = new Message("Hello World !!");
            Message Msg2 = new Message("Hello Universe !!");

            Msg1.PrintMsg();
            Msg2.PrintMsg();
        }
    }
}
  • We have two classes here. The Program class is the main class. If you run this program, it will run the Main method in this class.
  • Message class is used to hold a string.
  • Inside Main method, we are creating two objects of the Message class. Both of these objects are created with different strings.
  • We are calling PrintMsg method on these objects. This method prints the string that is assigned to the object while creating.

If you run the above program, it will print the below two lines:

Hello World !!
Hello Universe !!

Array of objects:

We can also create an array of objects as like below:

using System;

namespace c_sharp
{
    class Message
    {
        string Data;
        public Message(string Msg)
        {
            this.Data = Msg;
        }

        public void PrintMsg()
        {
            Console.WriteLine(this.Data);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Message Msg1 = new Message("Hello World !!");
            Message Msg2 = new Message("Hello Universe !!");
            Message Msg3 = new Message("Hello People !!");

            Message[] MessageArray = new Message[]{Msg1, Msg2, Msg3};
        }
    }
}

Here, we created one array with three objects Msg1, Msg2 and Msg3.

Another way to create an object array:

We can also create an empty array first and assign the objects for each index separately. For example:

Message Msg1 = new Message("Hello World !!");
Message Msg2 = new Message("Hello Universe !!");
Message Msg3 = new Message("Hello People !!");

Message[] MessageArray = new Message[3];
MessageArray[0] = Msg1;
MessageArray[1] = Msg2;
MessageArray[2] = Msg3;

The index of array starts from 0 and it ends at length - 1. So, we can assign the values by using the indices.

Accessing objects in an array:

We can use the index to access an item in an array. For example,

Message Msg1 = new Message("Hello World !!");
Message Msg2 = new Message("Hello Universe !!");
Message Msg3 = new Message("Hello People !!");

Message[] MessageArray = new Message[]{Msg1, Msg2, Msg3};

MessageArray[1].PrintMsg();

We are calling PrintMsg on the item at index 1. It will print Hello Universe, i.e. it will call PrintMsg on Msg2 object.

You might also like: