You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何遍历List<T>?解决foreach循环报错问题

C#泛型List遍历问题与Azure HTTP函数超时说明

一、C#遍历List的问题解决

你的代码报错核心原因是:泛型方法ShowLists<T>中,编译器无法推断T的具体类型,直接访问特定类(如Car、Cartoon)的属性会编译失败;同时直接强制转换元素类型存在运行时风险。

你需要安全地将泛型列表转换为特定类型列表,或采用更优雅的面向对象方式处理,以下是几种可行方案:

方案1:使用模式匹配直接转换列表(最贴合现有代码)

通过模式匹配is关键字,直接将List<T>转换为特定类型列表,编译器自动识别类型,无需额外强制转换:

public static void ShowLists<T>(List<T> list) 
{
    if (list is List<Car> cars) 
    {
        foreach (var car in cars) 
        {
            Console.WriteLine($"制造商:{car.Manufacturer},型号:{car.Model},产地:{car.Country}");
        }
    } 
    else if (list is List<Cartoon> cartoons) 
    {
        foreach (var cartoon in cartoons) 
        {
            Console.WriteLine($"动画标题:{cartoon.Title}");
        }
    }
}

方案2:实现统一接口(面向对象最佳实践)

定义统一展示接口,让Car和Cartoon类实现该接口,利用多态性避免类型判断:

public interface IDisplayable
{
    void Display();
}

public class Car : IDisplayable
{
    public string Manufacturer { get; set; }
    public string Model { get; set; }
    public string Country { get; set; }

    public void Display()
    {
        Console.WriteLine($"制造商:{Manufacturer},型号:{Model},产地:{Country}");
    }

    public List<Car> GetCars() 
    {
        var cars = new List<Car>();
        cars.Add(new Car() { Manufacturer = "VW", Model = "Phaeton", Country = "Germany" });
        cars.Add(new Car() { Manufacturer = "Land Rover", Model = "Range Rover Sport", Country = "UK" });
        return cars;
    }
}

public class Cartoon : IDisplayable
{
    public string Title { get; set; }

    public void Display()
    {
        Console.WriteLine($"动画标题:{Title}");
    }

    public List<Cartoon> GetCartoons() 
    {
        var cartoons = new List<Cartoon>();
        cartoons.Add(new Cartoon() { Title = "Family Guy" });
        cartoons.Add(new Cartoon() { Title = "American Dad" });
        return cartoons;
    }
}

public static void ShowLists<T>(List<T> list) where T : IDisplayable
{
    foreach (var item in list)
    {
        item.Display();
    }
}

方案3:重写ToString方法(轻量解决方案)

让每个类重写ToString方法,直接输出对象信息,泛型方法无需类型判断:

public class Car 
{
    public string Manufacturer { get; set; }
    public string Model { get; set; }
    public string Country { get; set; }

    public override string ToString()
    {
        return $"制造商:{Manufacturer},型号:{Model},产地:{Country}";
    }

    public List<Car> GetCars() 
    {
        var cars = new List<Car>();
        cars.Add(new Car() { Manufacturer = "VW", Model = "Phaeton", Country = "Germany" });
        cars.Add(new Car() { Manufacturer = "Land Rover", Model = "Range Rover Sport", Country = "UK" });
        return cars;
    }
}

public class Cartoon 
{
    public string Title { get; set; }

    public override string ToString()
    {
        return $"动画标题:{Title}";
    }

    public List<Cartoon> GetCartoons() 
    {
        var cartoons = new List<Cartoon>();
        cartoons.Add(new Cartoon() { Title = "Family Guy" });
        cartoons.Add(new Cartoon() { Title = "American Dad" });
        return cartoons;
    }
}

public static void ShowLists<T>(List<T> list) 
{
    foreach (var item in list)
    {
        Console.WriteLine(item);
    }
}

二、Azure HTTP函数超时问题说明

HTTP触发的Azure函数,无论函数应用内设置的超时时间是多少,最长响应请求时间限制为230秒,这是由Azure负载均衡器的默认空闲超时规则导致的——超过该时间,负载均衡器会主动断开与客户端的连接,即使函数仍在后台运行,客户端也会收到超时错误。

若需处理超过230秒的任务,建议采用异步队列模式:

  • 将长任务的请求内容存入Azure队列存储
  • 立即返回客户端"请求已接受"的响应
  • 由队列触发的Azure函数处理后台长任务
  • 任务完成后,通过邮件、Webhook等方式通知客户端结果

内容的提问来源于stack exchange,提问作者Arjun Karthikeyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 02:13:27