开发者

What is the significance of a return statement in a procedure of void type

开发者 https://www.devze.com 2023-02-09 21:02 出处:网络
Is there any significance for a return statement in a void procedure. I had a scenario where I am using some COM Interop and the runtime behaviour was strange. The code executes fine when I debug usi

Is there any significance for a return statement in a void procedure.

I had a scenario where I am using some COM Interop and the runtime behaviour was strange. The code executes fine when I debug using Visual Studio, But it misbehaves when executed from an exe. No exceptions are thrown in any case. My code was something like this.

private void function1()
{
                DialogResult dialogResult = MessageBox.Show(messageInfo.DisplayText, GlobalData.MessageCaption, MessageBoxButtons.OKCancel);
                if (dialogResult == DialogResult.OK)
                {
                    minSize = (int)numeric开发者_如何转开发Updown.Value;
                    return;
                }
                else
                {
                    minSize = sampleSize;
                    return; //This is the return statement in question
                }
}

The issue was resolved when I added a return statement inside the else block[There are no lines after the return statement.]. I confirmed by repeatedly testing the issue with and without the return statement, and am now confused about what exactly is happening.

Does this have any change in the way the application behaves.


The method will return and any remaining code will not execute. This depends on a few thing though, like whether the return is in a using or try-catch block - as certain things run even if you explicitly return.

Void return types just can't return anything.

As for your strange issue, assuming the code is entirely what you posted, then returning will do nothing special. I can only guess that you haven't posted all the code, which means the return might be doing something (as in, stopping the duff code from running).


I have also noticed different behaviour when debugging and when non-debbuging, also with COM, in System.DirectoryServices.

The different behaviour seems to have something to do with COM.

IMHO, it is either a compiler or a runtime bug.

0

精彩评论

暂无评论...
验证码 换一张
取 消