Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

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 257 views
257 views asked Aug 4, 2018 by avibootz
1 answer 91 views
1 answer 150 views
1 answer 122 views
1 answer 153 views
1 answer 132 views
...