gpt4 book ai didi

c# - IEnumerable 在与 namespace System.Collections 一起使用时报告编译器错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:20:40 25 4
gpt4 key购买 nike

我正在查看这个 Squares 扩展方法,它已经存在于 Internet 中。我无法编译。编译器报告类似“非泛型类型‘System.Collections.IEnumerable’不能与类型参数一起使用”

知道下面这段代码有什么问题吗?

非常感谢任何帮助。

using System.IO;
using System;
using System.Collections;

static class Program {

static IEnumerable<int> Squares (this int from, int to) {
for (int i=from;i<=to;i++)
{
yield return (int)i*i;
}
}

static void Main(string[] args)
{
var min=1;
foreach (int i in min.Squares(4))
{
Console.WriteLine(i);
}
}
}

最佳答案

using System.Collections; 替换为 using System.Collections.Generic;

关于c# - IEnumerable 在与 namespace System.Collections 一起使用时报告编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19398358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com