c#error:是一个'field',但在尝试使用while循环时用作'type'

Joe*_*rts -4 c# while-loop

我正在通过C#进行euler项目来学习语言并在暑假期间保持我的编码技能(我是大学生).所以我对这门语言很新.我试图寻找这个错误的答案,但我找到的所有其他类似问题都没有处理while循环.有问题的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EulerProblems
{
    class Problem2
    {
        List<int> fib = new List<int>();
        bool notAtLimit = true;

        while(notAtLimit)
        {
             //code to populate list of fibonacci series
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Fla*_*ric 8

您不能直接在类中编写代码.你需要在一个方法中写出它,例如

private void Test()
{
    List<int> fib = new List<int>();
    bool notAtLimit = true;

    while (notAtLimit)
    {
        //code to populate list of fibonacci series
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 方法,不是功能. (2认同)