c# Array Part 2 (Using reference types)
Submitted by jin29neci on Sunday, August 29, 2010 - 21:24.
Using arrays in c#, you can also declare arrays of custom types. In our example let’s start with a Customer class, having two constructors, 3 properties (FirstName, LastName, ContactNumber), and an override of ToString() method of the Object class.
Inside our main method, let’s try to use our custom type with arrays.

- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ArraysPart2cs
- {
- class Customer
- {
- #region -[Private Member Fields]-
- private string FirstName_;
- private string LastName_;
- private string ContactNumber_;
- #endregion
- //our first constructor
- public Customer()
- {
- }
- //our second constructor
- public Customer(string _FirstName, string _LastName, string _ContactNumber)
- {
- this.FirstName_ = _FirstName;
- this.LastName_ = _LastName;
- this.ContactNumber_ = _ContactNumber;
- }
- #region -[Properties]-
- public string FirstName
- {
- get { return this.FirstName_; }
- set { this.FirstName_ = value; }
- }
- public string LastName
- {
- get { return this.LastName_; }
- set { this.LastName_ = value; }
- }
- public string ContactNumber
- {
- get { return this.ContactNumber_; }
- set { this.ContactNumber_ = value; }
- }
- #endregion
- public override string ToString()
- {
- string FullName = string.Concat(this.LastName_, " ", this.FirstName_);
- return string.Format("You are : {0}, your contact number: {1}", FullName, this.ContactNumber_);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ArraysPart2cs
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Title = "Arrays Part 2";
- //so lets declare an array of customer having 2 elements
- //lets try a short method
- Console.WriteLine(customer[0].ToString());
- //lets try a long method
- customer[1].FirstName = "Jin";
- customer[1].LastName = "Zalzos";
- customer[1].ContactNumber = "+639083069011";
- Console.WriteLine(customer[1].ToString());
- //you may also use array initializer with custom types
- Console.WriteLine(anotherCustomer[0]);
- Console.ReadKey();
- }
- }
- }

Add new comment
- 33 views