In my WinForms app, I call Application.Exit()
in certain circumstances.
Application.Exit()
has an overload that looks like this:
Application.Exit(CancelEventArgs e)
And the documentation says that e
"Returns whether any Form within the application cancelled the exit."
However, it's unclear to me how I could ever examine e
. The method returns void, and e
is not defined as an out variable. Am I supposed to be able to examine this?
Yes, I did look at other questions regarding Application.Exit(), but none of them address this. They address handling the event, not calling the method.
Instantiate a CancelEventArgs
variable and test its Cancel
property after call to the Application.Exit
:
CancelEventArgs e = new CancelEventArgs();
Application.Exit(e);
if (e.Cancel)
{
// Cancelled
}
The reason you can examine e
is that e
is a reference to the CancelEventArgs
object you pass in the method call. A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter , in this case a reference to a CancelEventArgs
object, the called method is able to use the reference to access properties of the CancelEventArgs
object, and it may for example set e.Cancel
to true
.
After the method call completes, the story is the same: e
is still a reference to the CancelEventArgs
object you passed, and you may now examine its properties to establish whether any have been changed by the called method.
EDIT I see from your comment it's not yet clear, so consider this: passing e
as ref
would mean the called method could change e
to refer to a different CancelEventArgs
object. It has nothing to do with whether it can set properties in the existing CancelEventArgs
object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.