You have a few problems:
- Your return type definition was wrong: append the array (
[]
) to the type (double
), not to the method;
- Your array has size 1. It is hard to fit two items in it. I changed it to 2.
Now the working code:
public double[] Rectangle(int Length, int Width)
{
double[] rect = new double[2];
double areaOfRectangle = Length * Width;
double perimeterOfRectangle = 2 * (Length + Width);
rect[0] = areaOfRectangle;
rect[1] = perimeterOfRectangle;
return rect;
}
Call it like this:
MyFunctions mf = new MyFunctions();
double[] d = mf.Rectangle();
double areaOfRectangle = d[0];
double perimeterOfRectangle = d[1];
In other cases, you might need a Tuple
instance, custom class or struct instance as return type, or an out
parameter.
Something like this is an option too (With a custom class SpecialRectangle
):
public class SpecialRectangle
{
double AreaOfRectangle { get; set; }
double PerimeterOfRectangle { get; set; }
}
public SpecialRectangle Rectangle(double Length, double Width)
{
return new SpecialRectangle() { AreaOfRectangle = Length * Width, PerimeterOfRectangle = 2 * (Length + Width) };
}
Call it like this:
MyFunctions mf = new MyFunctions();
SpecialRectangle s = mf.Rectangle();
double areaOfRectangle = s.AreaOfRectangle;
double perimeterOfRectangle = s.PerimeterOfRectangle;