I was looking at the "Contose Cookbook" training for creating Windows 8.1 Apps.
Going through the first lab, I saw something like variable += function;
. The function didn't have paranthesis, and as far as I can see, you can create functions without paranthesis, which are properties, but you have to call them with.
Here is a class where I found this:
namespace ContosoCookbook
{
sealed partial class App : Application
{
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
}
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
await SuspensionManager.SaveAsync();
deferral.Complete();
}
...
}
}
What type of calling or function is that that accepts this.
The Application.Suspending event from msdn.
Without parentheses, the function name is used as the pointer of the function. The name of a function is the pointer of the function. At this time, the result of the function is not obtained, because the function body code will not be run.
It means you can call a function without parentheses if that function is no parameters. otherwise you need to include parentheses if it have parameters.
Without parentheses you're not actually calling the function. A function name without the parentheses is a reference to the function. We don't use the parentheses in that code because we don't want the function to be called at the point where that code is encountered.
Invoking FunctionsParentheses are necessary when you want to invoke functions. Calling on the name of a function without following it by parentheses will point towards the function object, but will not call the function itself. The code inside the body of the function will not get executed.
If I fire up my console and key in the following function (or method, depending where it is used)… Using () after a function means to execute the function and return it's value. Using no () means to fetch the function to be passed along as a callback.
The event handler in question takes a delegate which matches the method's arguments, allowing it to take a method group.
As a simple example, any Action
variable can accept any void method with no parameters as a method group. The above event handler's delegate likely takes an object
to indicate the sender and some sort of EventArguments
.
The most common delegates anymore are Action<T>
, Func<T>
and Predicate<T>
, which represent void methods, methods with a specified return type, and methods which return booleans respectively. They can also be assigned lambdas. All of them have varying numbers of parameters. They can be very useful in some situations.
Note that assigning a delegate does not invoke it; that can be done at a later time. Delegates can also be combined. It may also be worth noting that delegates can do strange things with local variables: if you assign a delegate with something like (name) => tempName += name
where tempName
is a local variable, your local variable will be modified on invocation of the delegate. This makes sense, but may not be obvious.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With