using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// 定义一个列表
List<string> fruits = new List<string> { "apple", "banana", "cherry" };
// 使用 "in" 关键字进行遍历
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
// 检查某个元素是否在集合中
string searchFruit = "banana";
if (fruits.Contains(searchFruit))
{
Console.WriteLine($"{searchFruit} is in the list.");
}
else
{
Console.WriteLine($"{searchFruit} is not in the list.");
}
}
}
定义一个列表:
List<string> fruits = new List<string> { "apple", "banana", "cherry" };
这里我们创建了一个包含三个水果名称的字符串列表。
使用 in
关键字进行遍历:
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
使用 foreach
循环和 in
关键字遍历列表中的每个元素,并打印出来。
检查某个元素是否在集合中:
string searchFruit = "banana";
if (fruits.Contains(searchFruit))
{
Console.WriteLine($"{searchFruit} is in the list.");
}
else
{
Console.WriteLine($"{searchFruit} is not in the list.");
}
使用 Contains
方法检查列表中是否包含指定的元素,并输出相应的结果。
上一篇:selenium c#
下一篇:c#??
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站