Keys[] keysArray = new Keys[5]; Keys key1 = Keys.A; Keys key2 = Keys.B; Keys key3 = Keys.C; Keys key4 = Keys.D; Keys key5 = Keys.E; Keys[] keysCollection = { key1, key2, key3, key4, key5 }; keysCollection.CopyTo(keysArray, 0);
var keysCollection = new HashSetIn this example, we use a HashSet to create a collection of Keys. We then use the CopyTo method to copy all the keys in the collection to a new array with a size equal to the count of keys in the collection. Package Library: System.Windows.Forms(); keysCollection.Add(Keys.F); keysCollection.Add(Keys.G); keysCollection.Add(Keys.H); Keys[] keysArray = new Keys[keysCollection.Count]; keysCollection.CopyTo(keysArray, 0);