How to declare an array of Custom Type in C#

No.of Views930
Bookmarked0 times
Downloads 
Votes0
By  Ravish Kumar Sindhwani   On  14 Jun 2010 08:06:32
Tag : CSharp , Miscellaneous
In this article we will see that how we can create arrays of custom types. We can achieve it by using custom types.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article we will see that how we can create arrays of custom types. We can achieve it by using custom types.We all know that array is a reference type i.e. memory for an array is allocated in a heap.Sometimes we need to declare arrays of custom types rather than arrays of predefined types ( int or string etc).

Following are the steps to declare an array of custom type,

Step 1 :

Start a new console application project

Step2:

Create a class Employee as shown below

class Employee
    {public string empName { set; get; }public string empAddress { set; get; }
    }

 Step3:

Declare the array of two employees in the same way as we are declaring it for predefined datatype.

Employee[] newEmployee = new Employee[2];

 

Image Loading

Here new employee is the array of Employee. Here we have given the size of this array as 2 so it will create two reference types. Each will point to Employee class.

Complete Code

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

namespace UsingRefTypeInArray
{class Program
    {static void Main(string[] args)
        {
            Employee[] newEmployee = new Employee[2];
            newEmployee[0] = new Employee { empName = "Ravish", empAddress = "Haryana" };
            newEmployee[1] = new Employee { empName = "Nihar", empAddress = "Udisa" };
            Console.WriteLine("Employee Details");foreach (var v in newEmployee)
            {
                Console.WriteLine("NAme:" + v.empName + " Address: "+  v.empAddress +"\nNext\n");
            }
            Console.Read();
        }
    }class Employee
    {public string empName { set; get; }public string empAddress { set; get; }
    }
}

 When we execute it than we will get the output in the below format: 

Image Loading

In this way we can create an array of custom types and retrieve the value from it.that's all enjoy.

 
Sign Up to vote for this article
 
About Author
 
Ravish Kumar Sindhwani
Occupation-Software Engineer
Company-
Member Type-Fresh
Location-India
Joined date-11 Jun 2010
Home Page-
Blog Page-
I born and brought up in Haryana and working as a software developer in Trivandrum. I am passionate for learning new Microsoft technologies and I believe if you are learning any new thing, just write it some where. You will never forget.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top