Loading

c# Array Part 2 (Using reference types)

Submitted by: 


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.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ArraysPart2cs
  7. {
  8. class Customer
  9. {
  10. #region -[Private Member Fields]-
  11.  
  12. private string FirstName_;
  13. private string LastName_;
  14. private string ContactNumber_;
  15.  
  16. #endregion
  17.  
  18. //our first constructor
  19. public Customer()
  20. {
  21.  
  22. }
  23. //our second constructor
  24. public Customer(string _FirstName, string _LastName, string _ContactNumber)
  25. {
  26. this.FirstName_ = _FirstName;
  27. this.LastName_ = _LastName;
  28. this.ContactNumber_ = _ContactNumber;
  29. }
  30.  
  31. #region -[Properties]-
  32.  
  33. public string FirstName
  34. {
  35. get { return this.FirstName_; }
  36. set { this.FirstName_ = value; }
  37. }
  38.  
  39. public string LastName
  40. {
  41. get { return this.LastName_; }
  42. set { this.LastName_ = value; }
  43. }
  44.  
  45. public string ContactNumber
  46. {
  47. get { return this.ContactNumber_; }
  48. set { this.ContactNumber_ = value; }
  49. }
  50.  
  51. #endregion
  52.  
  53. public override string ToString()
  54. {
  55. string FullName = string.Concat(this.LastName_, " ", this.FirstName_);
  56.  
  57. return string.Format("You are : {0}, your contact number: {1}", FullName, this.ContactNumber_);
  58.  
  59. }
  60. }
  61. }

Inside our main method, let’s try to use our custom type with arrays.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ArraysPart2cs
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12.  
  13. Console.Title = "Arrays Part 2";
  14.  
  15. //so lets declare an array of customer having 2 elements
  16.  
  17. Customer[] customer = new Customer[2];
  18.  
  19. //lets try a short method
  20.  
  21. customer[0] = new Customer("John Felix", "Cruz", "+639083069011");
  22.  
  23. Console.WriteLine(customer[0].ToString());
  24.  
  25. //lets try a long method
  26.  
  27. customer[1] = new Customer();
  28.  
  29. customer[1].FirstName = "Jin";
  30. customer[1].LastName = "Zalzos";
  31. customer[1].ContactNumber = "+639083069011";
  32.  
  33. Console.WriteLine(customer[1].ToString());
  34.  
  35.  
  36. //you may also use array initializer with custom types
  37.  
  38. Customer[] anotherCustomer = {new Customer("Rhian","Calzado","+63908306911"),
  39. new Customer("B. Togie", "Calzado", "+6301233655")};
  40.  
  41. Console.WriteLine(anotherCustomer[0]);
  42.  
  43. Console.ReadKey();
  44.  
  45. }
  46. }
  47. }

output



Download Code:: 

Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.