Initialize Array of Objects in C#

Initialize Array of Objects in C#

Saad Aslam Apr-14, 2022 Feb-11, 2022 Csharp Csharp Array
  1. Use the Constructor Parameters to Initialize an Array of Objects in C#
  2. Create get and set Methods to Complete the Constructor Call Activities
  3. the Whole Source Code to Initialize Array Through Constructor Parameters in C#

This article will introduce how to initialize an array of objects through Constructor parameters in C#.

Use the Constructor Parameters to Initialize an Array of Objects in C#

First, we must import the System library to use their functions in our example.

using System;

Then, within the public class Person, create fields, constructor, getter, setter methods, and the other methods. That will complete the constructor call activities and show the output.

public class Person{}

Inside the Person class, declare two private variables, named personge and personName, with data types long and string, respectively.

We are using private access modifiers for the variables to keep our data safe from leakages because we do not want our data to be accessed from outside this class.

private long personAge;
private string personName;

Then we’ll construct our constructor Person and pass it to two variables of data types long and string, respectively, labeled age and name.

We’ll assign the value of age to the previously created variable personge inside, which we’ll assign the value of age that will be obtained upon calling it. The personName variable will be handled similarly.

public Person(long age, string Name)
{
  personAge = age;
  personName = Name;
}

After this, create another class, Personge with the data type long, same as our personge variable’s data type.

Create get and set Methods to Complete the Constructor Call Activities

Inside the class, we’ll create a get method that will return personge every time we call it. Also, we’ll create a set method that will assign the value received on calling it to the personge variable.

public long PersonAge
{
  get
  {
    return personAge;
  }
  set
  {
    personAge = value;
  }
}

Similar to the previous class Personge, we’ll create another class, PersonName, inside which a get method will be created.

Which will return personName every time it is called, and a set method will assign the value received on calling it to the personName variable.

public string PersonName
{
  get
  {
    return personName;
  }
  set
  {
    personName = value;
  }
}

After these above steps, the Person class will be closed, and after that, we create another public class, BioData.

public class BioData{}

We’ll create a Person array class object with variable persons declaration inside this class.

Person[] persons;

Now, we’ll create a public method, BioData(), inside which we’ll initialize a Person array type variable named persons, and by calling Person constructor.

We’ll assign the required values personge and personName. We call the Person constructor 4 times and assign different values.

public BioData()
{
  persons = new Person[]
  {
  new Person (52, "Kian"),
  new Person (47, "Max"),
  new Person (19, "Franky"),
  new Person (76, "Connor"),
  };
}

After these steps are completed, we need to show the details of all the Persons. As a result, we’ll create a new method called showDetails() with the return type void because it doesn’t return anything.

First, this method will display Person's Details. Then we apply a foreach loop, and inside its condition part, we create a variable Detail.

Which will iterate the persons instances inside the loop’s body, we display Person’s Name and Person’s Age.

public void showDetails()
{
  Console.WriteLine("Person's Details\n");

  foreach (var Detail in persons)
  {
    Console.WriteLine("Person Name: " + Detail.PersonName);

    Console.WriteLine("Person Age: " + Detail.PersonAge);

    Console.WriteLine("---------------------------");
  }
}

Now, we have the int type main method. Inside the main, we initialize a variable Data and assign it the BioData() method located inside the BioData class.

We’ll use Data to execute the showDetails() method, which will display all of the Person’s attributes.

public static class PersonDetail
{
    static int Main(string[] args)
    {
        var Data = new BioData();
        Data.showDetails();
        return 0;
    }
}

the Whole Source Code to Initialize Array Through Constructor Parameters in C#

using System;
public class Person
{
    private long personAge;
    private string personName;

    public Person(long age, string Name)
    {
        personAge = age;
        personName = Name;
    }
    public long PersonAge
    {
        get
        {
            return personAge;
        }
        set
        {
            personAge = value;
        }
    }

    public string PersonName
    {
        get
        {
            return personName;
        }
        set
        {
            personName = value;
        }
    }
}

public class BioData
{
    Person[] persons;
    public BioData()
    {
        persons = new Person[]
        {
        new Person (52, "Kian"),
        new Person (47, "Max"),
        new Person (19, "Franky"),
        new Person (76, "Connor"),
        };
    }
    public void showDetails()
    {
        Console.WriteLine("Person's Details\n");

        foreach (var Detail in persons)
        {
            Console.WriteLine("Person Name: " + Detail.PersonName);

            Console.WriteLine("Person Age: " + Detail.PersonAge);

            Console.WriteLine("---------------------------");
        }
    }
}

public static class PersonDetail
{
    static int Main(string[] args)
    {
        var Data = new BioData();

        Data.showDetails();

        return 0;
    }
}

Output:

Person's Details

Person Name: Kian
Person Age: 52
---------------------------
Person Name: Max
Person Age: 47
---------------------------
Person Name: Franky
Person Age: 19
---------------------------
Person Name: Connor
Person Age: 76
---------------------------
Author: Saad Aslam
Saad Aslam avatar Saad Aslam avatar

I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.

LinkedIn

Related Article - Csharp Array

  • Get the Length of an Array in C#
  • Sort an Array in C#
  • Sort an Array in Descending Order in C#
  • Remove Element of an Array in C#
  • Convert a String to a Byte Array in C#
  • Adding Values to a C# Array