How to use class with array in C#

1 Answer

0 votes
using System;

namespace class_with_array
{
	class array
	{
		private int[] arr;
		private int len;
        
		public array()
		{
			len = 10;	
			arr = new int [10];
		}
		public array(int len)
		{
			this.len = len;
			arr = new int [len];
		}
		public void init()
		{
            Random rnd = new Random(DateTime.Now.Millisecond);

			for (int i = 0; i < len; i++)
				arr[i] = rnd.Next(1, 100);
		}
		public void show()
		{
			for (int i = 0; i < len; i++)
				Console.Write("{0, 3}", arr[i]);
			Console.WriteLine();
		}
	}
	class Class1
	{
		static void Main(string[] args)
		{
            array a1 = new array();
			array a2 = new array(20);

			a1.init();
			a2.init();

			a1.show(); // 26 61 54 17  2 40 27 15 44 93
			a2.show(); // 78 90 25 38 47 27 71 65 56 30 79 46 84 97 17 19 84 91  6 55
		}
	}
}



answered Jul 23, 2014 by avibootz

Related questions

2 answers 281 views
281 views asked Aug 4, 2018 by avibootz
1 answer 115 views
115 views asked Jun 21, 2024 by avibootz
1 answer 180 views
1 answer 148 views
1 answer 176 views
1 answer 156 views
...