public class CoordinateSystem { private int[] points; } Like any field, when an array has been declared as a member variable, it is made available to all the other members of the same class. You can use this feature to initialize the array in one method and let other methods use the initialized variable. This also means that you don't have to pass the array as argument nor do you have to explicitly return it from a method. After or when declaring an array, you must make sure you allocate memory for it prior to using. Unlike C++, you can allocate memory for an array when declaring it. Here is an example: public class CoordinateSystem { private int[] points = new int[4]; } You can also allocate memory for an array field in a constructor of the class. Here is an example: public class CoordinateSystem { private int[] points; public CoordinateSystem() { points = new int[4]; } } If you plan to use the array as soon as the program is running, you can initialize it using a constructor or a method that you know would be called before the array can be used. Here is an example: public class CoordinateSystem { private int[] points; public CoordinateSystem() { points = new int[4]; points[0] = 2; points[1] = 5; points[2] = 2; points[3] = 8; } }
After an array has been created as a field, it can be used by any other member of the same class. Based on this, you can use a member of the same class to request values that would initialize it. You can also use another method to explore the array. Here is an example: using System; public class CoordinateSystem { private int[] points; public CoordinateSystem() { points = new int[4]; points[0] = 2; points[1] = -5; points[2] = 2; points[3] = 8; } public void ShowPoints() { Console.WriteLine("Points Coordinates"); Console.WriteLine("P({0}, {1})", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); } } public class Exercise { static int Main() { var Coordinates = new CoordinateSystem(); Coordinates.ShowPoints(); return 0; } } This would produce: Points Coordinates P(2, -5) Q(2, 8) Press any key to continue . . .
Each member of an array holds a legitimate value. Therefore, you can pass a single member of an array as argument. You can pass the name of the array variable with the accompanying index to a method. The main purpose of using an array is to use various values grouped under one name. Still, an array is primarily a variable. As such, it can be passed to a method and it can be returned from a method.
Like a normal variable, an array can be returned from a method. This means that the method would return a variable that carries various values. When declaring or defining the method, you must specify its data type. When the method ends, it would return an array represented by the name of its variable. You can create a method that takes an array as argument and returns another array as argument. To declare a method that returns an array, on the left of the method's name, provide the type of value that the returned array will be made of, followed by empty square brackets. Here is an example: using System; public class CoordinateSystem { public int[] Initialize() { } } Remember that a method must always return an appropriate value depending on how it was declared. In this case, if it was specified as returning an array, then make sure it returns an array and not a regular variable. One way you can do this is to declare and possibly initialize a local array variable. After using the local array, you return only its name (without the square brackets). Here is an example: using System; public class CoordinateSystem { public int[] Initialize() { var coords = new int[] { 12, 5, -2, -2 }; return coords; } } When a method returns an array, that method can be assigned to an array declared locally when you want to use it. Remember to initialize a variable with such a method only if the variable is an array. Here is an example: using System; public class CoordinateSystem { public int[] Initialize() { var coords = new int[] { 12, 5, -2, -2 }; return coords; } } public class Exercise { static int Main() { var system = new int[4]; var coordinates = new CoordinateSystem(); system = coordinates.Initialize(); return 0; } } The method could also be called as follows: public class Exercise { static int Main() { var coordinates = new CoordinateSystem(); var system = coordinates.Initialize(); return 0; } } If you initialize an array variable with a method that doesn't return an array, you would receive an error.
Like a regular variable, an array can be passed as argument. To proceed, in the parentheses of a method, provide the data type, the empty square brackets, and the name of the argument. Here is an example: public class CoordinateSystem { public void ShowPoints(int[] points) { } } When an array has been passed to a method, it can be used in the body of the method as any array can be, following the rules of array variables. For example, you can display its values. The simplest way you can use an array is to display the values of its members. This could be done as follows: public class CoordinateSystem { public void ShowPoints(int[] points) { Console.WriteLine("Points Coordinates"); Console.WriteLine("P({0}, {1})", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); } } To call a method that takes an array as argument, simply type the name of the array in the parentheses of the called method. Here is an example: using System; public class CoordinateSystem { public void ShowPoints(int[] points) { Console.WriteLine("Points Coordinates"); Console.WriteLine("P({0}, {1})", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); } } public class Exercise { static int Main() { var points = new int[] { -3, 3, 6, 3 }; var coordinates = new CoordinateSystem(); coordinates.ShowPoints(points); return 0; } } This would produce: Points Coordinates P(-3, 3) Q(6, 3) Press any key to continue . . . When an array is passed as argument to a method, the array is passed by reference. This means that, if the method makes any change to the array, the change would be kept when the method exits. You can use this characteristic to initialize an array from a method. Here is an example: using System; public class CoordinateSystem { public void Initialize(int[] coords) { coords[0] = -4; coords[1] = -2; coords[2] = -6; coords[3] = 3; } public void ShowPoints(int[] points) { Console.WriteLine("Points Coordinates"); Console.WriteLine("P({0}, {1})", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); } } public class Exercise { static int Main() { var system = new int[4]; var coordinates = new CoordinateSystem(); coordinates.Initialize(system); coordinates.ShowPoints(system); return 0; } } This would produce: Points Coordinates P(-4, -2) Q(-6, 3) Press any key to continue . . .
Consider the following code from what we saw earlier: using System; public class CoordinateSystem { public void ShowPoints(int[] points) { Console.WriteLine("In ShowPoints() - Points Coordinates"); Console.WriteLine("P({0}, {1})", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); } } public class Exercise { static int Main() { var system = new int[4]; var coordinates = new CoordinateSystem(); Console.WriteLine("In Main() - Points Coordinates"); Console.WriteLine("P({0}, {1})", system[0], system[1]); Console.WriteLine("Q({0}, {1})", system[2], system[3]); coordinates.ShowPoints(system); Console.WriteLine("In Main() - Points Coordinates"); Console.WriteLine("P({0}, {1})", system[0], system[1]); Console.WriteLine("Q({0}, {1})", system[2], system[3]); return 0; } } This would produce: In Main() - Points Coordinates P(0, 0) Q(0, 0) In ShowPoints() - Points Coordinates P(0, 0) Q(0, 0) In Main() - Points Coordinates P(0, 0) Q(0, 0) Press any key to continue . . . Notice that the ShowPoints() method receives an array whose elements don't have values. The array is initialized in Main(). An array is a reference-type. This means that when an element of an array is accessed, it is actually the memory address of where it resides that is accessed, not the value itself. Consequently, when an array is passed as argument, it is automatically passed by reference. This means that the method that receives the argument can change it and if it does, the value of the element is permanently changed. Consider the following code: using System;
public class CoordinateSystem
{
public void ShowPoints(int[] points)
{
points[0] = 2;
points[1] = -4;
points[2] = 6;
points[3] = 3;
Console.WriteLine("In ShowPoints() - Points Coordinates");
Console.WriteLine("P({0}, {1})", points[0], points[1]);
Console.WriteLine("Q({0}, {1})", points[2], points[3]);
}
}
public class Exercise
{
static int Main()
{
var system = new int[4];
var coordinates = new CoordinateSystem();
Console.WriteLine("In Main() - Points Coordinates");
Console.WriteLine("P({0}, {1})", system[0], system[1]);
Console.WriteLine("Q({0}, {1})", system[2], system[3]);
coordinates.ShowPoints(system);
Console.WriteLine("In Main() - Points Coordinates");
Console.WriteLine("P({0}, {1})", system[0], system[1]);
Console.WriteLine("Q({0}, {1})", system[2], system[3]);
return 0;
}
}
This would produce: In Main() - Points Coordinates P(0, 0) Q(0, 0) In ShowPoints() - Points Coordinates P(2, -4) Q(6, 3) In Main() - Points Coordinates P(2, -4) Q(6, 3) Press any key to continue . . . Again, notice that the ShowPoints() function receives an array without values but it changes it. When this function exits, the elements of the array have received values and Main() gets back the array with new values. It is important to understand that this ShowPoints() method doesn't initialize the array. It only changes the values of the elements of the array. This is further illustrated in the following code: using System; public class CoordinateSystem { public void ShowPoints(int[] points) { Console.WriteLine("Entering ShowPoints() - Points Coordinates"); Console.WriteLine("P({0}, {1})", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); points[0] = 2; points[1] = -4; points[2] = 6; points[3] = 3; Console.WriteLine("Exiting ShowPoints() - Points Coordinates"); Console.WriteLine("P({0}, {1})", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); } } public class Exercise { static int Main() { var system = new int[4]; var coordinates = new CoordinateSystem(); system[0] = -10; system[1] = 0; system[2] = 5; system[3] = -2; Console.WriteLine("Starting in Main() - Points Coordinates"); Console.WriteLine("P({0}, {1})", system[0], system[1]); Console.WriteLine("Q({0}, {1})", system[2], system[3]); coordinates.ShowPoints(system); Console.WriteLine("After Calling ShowPoints: In Main() - Points Coordinates"); Console.WriteLine("P({0}, {1})", system[0], system[1]); Console.WriteLine("Q({0}, {1})", system[2], system[3]); return 0; } } This would produce: Starting in Main() - Points Coordinates P(-10, 0) Q(5, -2) Entering ShowPoints() - Points Coordinates P(-10, 0) Q(5, -2) Exiting ShowPoints() - Points Coordinates P(2, -4) Q(6, 3) After Calling ShowPoints: In Main() - Points Coordinates P(2, -4) Q(6, 3) Press any key to continue . . . Still, if you want, you can re-initialize the array by allocating new memory to it. To enforce the concept of passing an array by reference, you can accompany the argument with the ref keyword, both when defining the method and when calling it. Here is an example: using System; public class CoordinateSystem { public void ShowPoints(ref int[] points) { Console.WriteLine("In ShowPoints() - Points Coordinates"); Console.WriteLine("P({0}, {1})", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); } } public class Exercise { static int Main() { var system = new int[4]; var coordinates = new CoordinateSystem(); system[0] = -10; system[1] = 0; system[2] = 5; system[3] = -2; coordinates.ShowPoints(ref system); Console.WriteLine("In Main() - Points Coordinates"); Console.WriteLine("P({0}, {1})", system[0], system[1]); Console.WriteLine("Q({0}, {1})", system[2], system[3]); return 0; } } Instead of just one, you can create a method that receives more than one array and you can create a method that receives a combination of one or more arrays and one or more regular arguments. You can also create a method that takes one or more arrays as argument(s) and returns a regular value of a primitive type.
As seen for passing an argument by reference, you can pass an array out. We saw that, when passing an array using the ref keyword, the method that receives the array doesn't have to initialize it. If the array was already initialized by the function that is making the call, the called method can simply change the values of the elements of the array. On the other hand, if you pass an array using the out keyword, the method that receives the out array must initialize it before exiting. Here is an example: using System; public class CoordinateSystem { public void ShowPoints(out int[] points) { points = new int[4]; points[0] = -1; points[1] = 7; points[2] = 2; points[3] = -2; Console.Write("In ShowPoints() - Points Coordinates: "); Console.Write("P({0}, {1}), ", points[0], points[1]); Console.WriteLine("Q({0}, {1})", points[2], points[3]); } } public class Exercise { static int Main() { var system = new int[4]; var coordinates = new CoordinateSystem(); coordinates.ShowPoints(out system); Console.Write("In Main() - Points Coordinates: "); Console.Write("P({0}, {1}), ", system[0], system[1]); Console.WriteLine("Q({0}, {1})", system[2], system[3]); return 0; } } This would produce: In ShowPoints() - Points Coordinates: P(-1, 7), Q(2, -2) In Main() - Points Coordinates: P(-1, 7), Q(2, -2) Press any key to continue . . . Passing an array as argument by reference makes it possible for a method to return many values.
We saw that you can pass an array as argument, either regularly or by reference. When you call the method that receives that argument, you must pass an array variable to it. We also saw that you can pass more than one array as argument and you can pass a combination of arrays and non-array arguments. When you call the method, you must pass the exact number of arguments. That is, you must know the number of arguemnts the method will process. An alternative to this is to pass only one array as argument. Then, when, or every time, you call the method, you can pass any number of values you want. That is, at one time you can call the method and pass 2 values. At another time you can call the same method but pass 8 arguments to it. To create a method that receives a varied number of arguments, in the parentheses of the method, type the params keyword followed by an array. Here is an example: public class CoordinateSystem { public void ShowPoints(params int[] points) { } } As mentioned already, when calling the method, you can pass the number of arguments you want. It's important to know that the method that receives a params argument doesn't know how many arguments it will receive when it is called. This means that you must find a way to access the arguments and you must find a way for the method to know the number of arguments it received. Because this information is not known to the method, the Array class provides a property named Length that holds the size of the array. Here are examples of calling a method that receives a params argument: using System; public class CoordinateSystem { public void ShowPoints(params int[] points) { int dimension = points.Length; if (dimension == 1) Console.WriteLine("The point is located on a line"); else if (dimension == 2) Console.WriteLine("The point is located on a Cartesian coordinate system"); else if (dimension == 3) Console.WriteLine("The point is located on a 3-D coordinate system"); else Console.WriteLine("The point is located on a multi-dimensional system"); } } public class Exercise { static int Main() { var system = new int[4]; var coordinates = new CoordinateSystem(); // The method is called with one argument coordinates.ShowPoints(-6); // The method is called with 4 arguments coordinates.ShowPoints(2, 2, 5, -3); // The method is called with two arguments coordinates.ShowPoints(2, 5); // The method is called with three arguments coordinates.ShowPoints(-4, 3, 1); return 0; } } This would produce: The point is located on a line The point is located on a multi-dimensional system The point is located on a Cartesian coordinate system The point is located on a 3-D coordinate system Press any key to continue . . . If you decide to create a method that receives a params argument, you can pass only one argument and that would be only the params argument.
|
|||||||||||||||||||||||||||||||||||||||||||||||||