C# program to initialize an array of objects:
In this post, we will learn how to initialize an array of objects in C#. We will create a class and different objects of that class. All of these objects will be inserted into an array.
With this program, you will learn how to create classes and how to initialize different objects of that class. We will also learn how to insert and iterate through these objects and print the values. If you already have an idea of classes and objects in C#, you will find it easier to understand. Let’s write down the program.
C# program to create an array of objects:
You can think of a class as a blueprint that specifies what it can do. Objects are blocks of memories allocated based on the class blueprint. We can create multiple objects for the same class. These are also called instances of the class.
In this example, we will create one Student class with different properties to hold student info and we will create different objects of this class. Once all objects are created, the program will initialize an array with these objects.
We will also learn how to iterate through this array of objects.
2 different C# programs to create an array of objects:
Below C# program creates an array of objects:
namespace Program
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public char Grade { get; set; }
public Student(string name, int age, char grade)
{
Name = name;
Age = age;
Grade = grade;
}
public void PrintStudentInfo()
{
Console.WriteLine("Name: {0}, Age: {1}, Grade: {2}", Name, Age, Grade);
}
}
class Program
{
static void Main(string[] args)
{
Student[] StudentArray = { new Student("Alex", 21, 'A'), new Student("Bob", 22, 'B') };
StudentArray[0].PrintStudentInfo();
StudentArray[1].PrintStudentInfo();
}
}
}
Here,
- The Student is a class that can hold three properties: Name, Age and Grade. Name is a string, Age is an integer and Grade is a character variable.
- In the constructor of the class, we are initializing it with the values of name, age and grade.
- The PrintStudentInfo method is printing the values of the Student class.
- One array, StudentArray is created with two Student objects. Both of these objects are initialized with different values.
- We are using the index values to access each of these items and calling PrintStudentInfo method to print the values.
If you run this program, it will print:
Name: Alex, Age: 21, Grade: A
Name: Bob, Age: 22, Grade: B
You can also create a new array of two items and assign values to each of these items separately.
namespace Program
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public char Grade { get; set; }
public Student(string name, int age, char grade)
{
Name = name;
Age = age;
Grade = grade;
}
public void PrintStudentInfo()
{
Console.WriteLine("Name: {0}, Age: {1}, Grade: {2}", Name, Age, Grade);
}
}
class Program
{
static void Main(string[] args)
{
Student[] StudentArray = new Student[2];
StudentArray[0] = new Student("Alex", 21, 'A');
StudentArray[1] = new Student("Bob", 22, 'B');
StudentArray[0].PrintStudentInfo();
StudentArray[1].PrintStudentInfo();
}
}
}
It will print a similar result.
How to iterate through an array of objects:
We can use any loop to iterate through an array of objects and access the element values. For example, the below program uses a foreach loop to iterate and print the details of each object:
namespace Program
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public char Grade { get; set; }
public Student(string name, int age, char grade)
{
Name = name;
Age = age;
Grade = grade;
}
public void PrintStudentInfo()
{
Console.WriteLine("Name: {0}, Age: {1}, Grade: {2}", Name, Age, Grade);
}
}
class Program
{
static void Main(string[] args)
{
Student[] StudentArray = { new Student("Alex", 21, 'A'), new Student("Bob", 22, 'B'), new Student("Daisy", 20, 'A') };
foreach (Student s in StudentArray)
{
s.PrintStudentInfo();
}
}
}
}
The StudentArray array holds three Student objects and the foreach loop iterate over the array and calls the PrintStudentInfo method on each of these objects to print the details of each object.
You might also like:
- 3 ways to find the nth number of the Fibonacci series
- 3 ways in C# to print the Arithmetic Progression or AP
- 4 ways in C# to print the Geometric Progression or GP
- 4 ways in C# to convert a hexadecimal value to Decimal
- C# program to convert a string to a character array
- 4 ways in C# to reverse an array
- How to get the length of an array in C#
- 3 ways in C# to remove elements from an array