CS1023嵌入式语句不能是声明或带标签的语句

时间:2019-02-18 20:03:35

标签: c#

我有一个可以正常工作的程序。这是正确的代码。

public static double Smth(int x, int y, double[,] original, int lengthX, int lengthY)
        {
            var pixelsFields = new List<double>();
            double median;
            for (int areasX = -1; areasX < 2; areasX++)
                for (int areasY = -1; areasY < 2; areasY++)
                    if ((x + areasX > -1) && (y + areasY > -1) && (x + areasX < lengthX) && (y + areasY < lengthY))
                        pixelsFields.Add(original[x + areasX, y + areasY]);

但是,我想改进我的代码。表达式“ x + areaX”和“ y + areaY”经常重复,因此我决定创建两个变量。不幸的是,我在新变量上遇到错误:“错误CS1023:嵌入式语句不能是声明或带标签的语句”。

public static double Smth(int x, int y, double[,] original, int lengthX, int lengthY)
            {
                var pixelsFields = new List<double>();
                double median;
                for (int areasX = -1; areasX < 2; areasX++)
                    for (int areasY = -1; areasY < 2; areasY++)
                        var diffX = x + areasX;
                        var diffY = y + areasY;
                        if ((diffX > -1) && (diffY > -1) && (diffX < lengthX) && (diffY < lengthY))
                            pixelsFields.Add(original[diffX, diffY]);

您能帮我改正吗?

2 个答案:

答案 0 :(得分:0)

for循环后您缺少bracets

 public static double Smth(int x, int y, double[,] original, int lengthX, int lengthY)
   {
        var pixelsFields = new List<double>();
        double median;
        for (int areasX = -1; areasX < 2; areasX++)
            for (int areasY = -1; areasY < 2; areasY++)
            {
                var diffX = x + areasX;
                var diffY = y + areasY;
                if ((diffX > -1) && (diffY > -1) && (diffX < lengthX) && (diffY < lengthY))
                    pixelsFields.Add(original[diffX, diffY]);
            }

答案 1 :(得分:0)

在C#中,iffor之类的结构仅捕获下一条语句,而与空白无关。当您写时:

var pixelsFields = new List<double>();
double median;
for (int areasX = -1; areasX < 2; areasX++)
    for (int areasY = -1; areasY < 2; areasY++)
        var diffX = x + areasX;
        var diffY = y + areasY;
        if ((diffX > -1) && (diffY > -1) && (diffX < lengthX) && (diffY < lengthY))
            pixelsFields.Add(original[diffX, diffY]);

该语言将其解释为以下内容(我们假装空格/缩进很重要):

var pixelsFields = new List<double>();
double median;
for (int areasX = -1; areasX < 2; areasX++)
    for (int areasY = -1; areasY < 2; areasY++)
        var diffX = x + areasX;
var diffY = y + areasY;
if ((diffX > -1) && (diffY > -1) && (diffX < lengthX) && (diffY < lengthY))
    pixelsFields.Add(original[diffX, diffY]);

如其他答案所述,解决方案是使用括号{},例如:

var pixelsFields = new List<double>();
double median;
for (int areasX = -1; areasX < 2; areasX++)
{
    for (int areasY = -1; areasY < 2; areasY++)
    {
        var diffX = x + areasX;
        var diffY = y + areasY;
        if ((diffX > -1) && (diffY > -1) && (diffX < lengthX) && (diffY < lengthY))
        {
            pixelsFields.Add(original[diffX, diffY]);
        }
    }
}

大多数样式指南都要求您使用此类花括号。如果不这样做,您可能会在添加另一条语句时无意中使处于循环中的语句不再处于该循环中,如本例所示。

对于您要得到的错误,内部for循环只有一个语句:var diffX = x + areasX;。之所以出现CS1023,是因为该单个语句是变量声明。该语言禁止这样做,可能是为了避免我提到的问题,并且可能是因为这样的变量的作用域是立即结束还是到达下一个},在语法上是模棱两可的。