是什么导致了这个"意外的'其他'"错误?

hug*_*dan 2 c#

在下面的代码中,Visual Studio会在其他单词上添加错误消息.特定错误显示为"Unexpected'else'".我做错了什么?

            decimal AmountToAccrue;
            string BillingDescription;


            if (PromoPeriodEnd >= day)

                AmountToAccrue = 0;
                BillingDescription = "Subscription 30-day Promotional Period";

            else

                AmountToAccrue = subscription.Amount * ProratedPercentDue;
                BillingDescription = "Subscription Fee";
Run Code Online (Sandbox Code Playgroud)

Jam*_*ill 17

当你有多行ifelse's 时,你必须使用花括号:

if (PromoPeriodEnd >= day)
{
    AmountToAccrue = 0;
    BillingDescription = "Subscription 30-day Promotional Period";
}
else
{
    AmountToAccrue = subscription.Amount * ProratedPercentDue;
    BillingDescription = "Subscription Fee";
}
Run Code Online (Sandbox Code Playgroud)

附加信息:

如果没有花括号,则只在范围内考虑下一个语句而不是行.

好:

if (PromoPeriodEnd >= day)
    AmountToAccrue = 0;
else
    AmountToAccrue = subscription.Amount * ProratedPercentDue;
Run Code Online (Sandbox Code Playgroud)

不好:

if (PromoPeriodEnd >= day)
    AmountToAccrue = 0;
    BillingDescription = "Subscription 30-day Promotional Period";
else
    AmountToAccrue = subscription.Amount * ProratedPercentDue;
    BillingDescription = "Subscription Fee";
Run Code Online (Sandbox Code Playgroud)

编译器将查看"不正常"示例,如下所示:

//Begin if statement
if (PromoPeriodEnd >= day)
    AmountToAccrue = 0;
//End if statement   

//Set BillingDescription  (outside of the else scope)
BillingDescription = "Subscription 30-day Promotional Period";

//Begin else statement - ERROR! Where is the matching if?
else
    AmountToAccrue = subscription.Amount * ProratedPercentDue;

//Set BillingDescription(outside of the else scope) - error above - never reached
BillingDescription = "Subscription Fee";
Run Code Online (Sandbox Code Playgroud)