array of objects in C#

C# Array of Objects: Learn how to announce an array of items in C#? How to get to techniques for class utilizing objects? Clarify the idea of an Array of Objects with an Example.

As we realize that, we can make an array of integers, coasts, copies and so on. Likewise, we can make an array of articles.

By utilizing this array of items, we can get to techniques for class with each article (which are the components of that array).

Think about the example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
	class Student
	{
		//private data members
		private int     rollno  ;
		private string  name    ;
		private int     age     ;       

		//method to set student details
		public void SetInfo(string name, int rollno, int age) 
		{
			this.rollno = rollno  ;
			this.age  = age;
			this.name = name;
		}
		
		//method to print student details
		public void printInfo()
		{
			Console.WriteLine("Student Record: ");
			Console.WriteLine("\tName     : " + name  );
			Console.WriteLine("\tRollNo   : " + rollno);
			Console.WriteLine("\tAge      : " + age   );
		}

	}

	class Program
	{
		static void Main()
		{
			//creating array of objects
			Student[] S = new Student[2];
			//initlising objects by detauls/inbuilt constructors
			S[0] = new Student();
			S[1] = new Student();
			//reading and printing first object
			S[0].SetInfo("Herry", 101, 25);
			S[0].printInfo();
			//reading and printing second object
			S[1].SetInfo("Potter", 102, 27);
			S[1].printInfo();

		}
	}
}

Output:

Student Record:
        Name     : Herry
        RollNo   : 101
        Age      : 25
Student Record:
        Name     : Potter
        RollNo   : 102
        Age      : 27

Here, we made a class for understudy and made an array of items to peruse, print the subtleties of the understudies.

Leave a Comment

error: Alert: Content is protected!!