【问题标题】:Using return with method inline使用内联方法返回
【发布时间】:2019-08-28 12:18:29
【问题描述】:

我想用switch 块内联一个方法。

如果我在switch 语句中使用return,它会破坏ParentMethod 的执行吗?

void ParentMethod()
{
    InliningMethod(myEnum.SomeValue);

    //do some after inline for test case
    Console.WriteLine("Will this have been writed?"); 
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
void InliningMethod(MyEnum value)
{
    switch(myEnum)
    {
        case MyEnum.SomeValue:
            return; //will it have been moved in ParentMethod?
        default: break;       
    }
    //do some
}

【问题讨论】:

    标签: c# inline


    【解决方案1】:

    内联并不意味着代码只是被复制过来。编译器会做对的,return 只会退出它所在的方法,不管编译器以后是否内联它。

    【讨论】:

      【解决方案2】:

      内联方法只是用更少的代码编写方法的一种风格,它不会改变执行原理。因此,内联方法中的“返回语句”不会破坏父方法。

      【讨论】:

        猜你喜欢
        • 2020-05-21
        • 2020-10-03
        • 2012-04-15
        • 1970-01-01
        • 1970-01-01
        • 2016-08-25
        • 2015-06-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多