Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# in

作者:炼狱死神   发布日期:2025-08-30   浏览:105

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.");
        }
    }
}

解释说明

  1. 定义一个列表:

    List<string> fruits = new List<string> { "apple", "banana", "cherry" };

    这里我们创建了一个包含三个水果名称的字符串列表。

  2. 使用 in 关键字进行遍历:

    foreach (string fruit in fruits)
    {
        Console.WriteLine(fruit);
    }

    使用 foreach 循环和 in 关键字遍历列表中的每个元素,并打印出来。

  3. 检查某个元素是否在集合中:

    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#??

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

c# rectangle

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站