In the following italicized code, why don’t we put “IntIndexer” in front of myData = new string[size]; since Customer cust = new Customer(); (assuming Customer is the name of the class):
*Customer cust = new Customer();*
using System;
/// <summary>
/// A simple indexer example.
/// </summary>
class IntIndexer
{
private string[] myData;
public IntIndexer(int size)
{
*myData = new string[size];*
for (int i = 0; i < size; i++)
{
myData[i] = "empty";
}
}
To break this down:
This can be broken into two parts:
The first line says that the name
custwill be able to refer to objects of typeCustomer. The second line creates a newCustomerobject and makescustrefer to it.The other example you give is already broken into those two parts:
and:
If the array of strings was to be of a constant length, we could collapse this onto one line as well, in
IntIndexer(before the constructor).But we need to use the
sizepassed into theIntIndexerconstructor, so we have to split the declaration and initialization into two steps.