C# params constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Madmartigan
    New Member
    • Dec 2006
    • 23

    C# params constructor

    Hello

    The script I have below does what I've been asked :

    Write a method that accepts an array of numbers as a parameter. The method should multiply each number by ten, and display the results. Call this method, passing to it an array of any three integers.

    What I'd like to know is if this is correct or if there is a better way to do this?

    Code:
     using System;
     class Array
     {
     	int int1;
     	int int2;
     	int int3;
     	
     	public Array(params int[] numbers)
     	{
     		int1	=	numbers[0];
     		int2	=	numbers[1];
     		int3	=	numbers[2];
      	
    		
     		Console.WriteLine("Number 1: " + (int1 * 10));
     		Console.WriteLine("Number 2: " + (int2 * 10));
     		Console.WriteLine("Number 3: " + (int3 * 10));
     	
     	}			
     				
     }
     
     public class ArrayNumbers
     {
     	public static void Main()
     	{
     		Array myArray = new Array(23, 2, 10);
     	}
     }
    Thanks

    M
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    That is a much more complex method then I think you would need.
    A simple function that takes in an int[] as an argument seems like it would be fine.


    We do not do homework assignments here, so you will have to look up how to use an int[] yourself. (Pretty basic, there should be plenty of examples online)

    Comment

    Working...