Assuming you have a grid control with the name MainGrid
(it doesn't have to be a grid control, anything will work. Just adjust your code accordingly)
int[] numbers = MainGrid.Children.OfType<Button>()
.Select(x => Convert.ToInt32(x.Content)).ToArray();
Here's some example XAML that goes with the above code
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="1" x:Name="F1" />
<Button Grid.Row="1" Grid.Column="0" Content="2" x:Name="F2" />
<Button Grid.Row="2" Grid.Column="0" Content="3" x:Name="F3" />
<Button Grid.Row="3" Grid.Column="0" Content="Do not count me" />
</Grid>
As LordTakkera pointed out in the comments, this code would fail if you had other buttons in the same parent container that you didn't want to get the value of. In such a case, you'd need an additional Where()
clause, which would look like this:
int[] numbers = MainGrid.Children.OfType<Button>()
.Where(x => x.Name.Contains("F"))
.Select(x => Convert.ToInt32(x.Content))
.ToArray();
Since LINQ can be confusing and unclear if you're not familiar with it, here's a more traditional way of doing the same thing:
//Unless you have a specific reason for wanting an int array, a list is easier to work with
List<int> numbers = new List<int>();
//Use .OfType<>() So that you don't have to cast each control to a button in the loop
foreach(Button button in MainGrid.Children.OfType<Button>())
{
//If it's not one of our named buttons, continue the loop
if(! button.Name.StartsWith("F")) continue;
int buttonValue = Convert.ToInt32(button.Content);
numbers.Add(buttonValue);
}