错误CS0116:命名空间不能直接包含字段或方法等成员-使用记事本手工编写代码
本文关键字:成员 记事本 代码 方法 不能 命名空间 CS0116 字段 包含 错误 | 更新日期: 2024-09-19 18:12:55
使用foreach在第16行获取错误。我的教授发电子邮件的速度不够快,预产期还有几个小时!我想我少了一张单子什么的。我认为替身后的d是不正确的。需要任何帮助!
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class Starbucks
{
public static void Main()
{
double[] x = {4.2, 5.7, 3.6, 9.1, 2.7, 8.4 };
}
}
static void MyGenerics(double x)
{
foreach (double d in x)
{
MessageBox.Show(x);
}
}
这是因为您在类Starbucks
之外定义了MyGenerics()
方法。在课堂内移动它。错误消息正是在告诉我们同样的事情。你的代码应该看起来像
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class Starbucks
{
public static void Main()
{
double[] x = {4.2, 5.7, 3.6, 9.1, 2.7, 8.4 };
MyGenerics(x);
}
static void MyGenerics(double[] xx)
{
foreach (double d in xx)
{
MessageBox.Show(d);
}
}
}
这个问题可能是由错误的缩进引起的。例如:
using System;
namespace Interrogacion_1.Model
{
interface IClass
{
public string Name { get; set; }
}
{
Console.WriteLine("HELLO WORLD"); #here error cs0116
}
}
正确的方法是:
using System;
namespace Interrogacion_1.Model
{
interface IClass
{
public string Name { get; set; }
public void Imprimir()
{
Console.WriteLine("HELLO WORLD");
}
}
}