C#通过foreach语句搜索数组元素的代码

内容过程中,将内容过程中常用的内容做个备份,下面的内容段是关于C#通过foreach语句搜索数组元素的内容,希望能对小伙伴们有所用途。

using System; 

public class Search { 
  public static void Main() { 
    int[] nums = new int[10]; 
    int val; 
    bool found = false; 

    for(int i = 0; i < 10; i++)  
      nums[i] = i; 

    val = 5; 

    foreach(int x in nums) { 
      if(x == val) { 
        found = true; 
        break; 
      } 
    } 

    if(found)  
      Console.WriteLine("Value found!"); 
  } 
}

猜你喜欢

转载自blog.51cto.com/14137986/2343171