-3

I've recently started learning C# and now I am making a Windows Form Application.

First I have an array of int here:

int[] myArray = new int[] {1, 2, 3, 4, 5, 6, 7};

and also, I want to read the value (int) that user entered. Here I declare it as below.

int userInput = Textbox.Text;

What I want to do with these two things is, among the values in myArray, I want to find out the closest one to the value of userInput.

And I want to store the closest number in another array, because I will repeat the this same action for several times and analyze which number came up most often.

I apologize for my unclear explanation, I'm still learning English. Thank you!

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Y.Tomioka
  • 13
  • 1
  • 2

1 Answers1

1
int usernumber = 3; //Get input from textbox
int[] myArray = new int[] { 1, 2, 3, 4, 5, 6, 7 };
var nearest = myArray.OrderBy(x => Math.Abs((long)x - usernumber)).First();
jignesh
  • 1,639
  • 1
  • 16
  • 18