I was reviewing the “Exercise 1: Building Your First Windows Azure Application” inside the Microsoft Azure Training Kit and ran into this code snippet that is a bit unclear.
The inbuilt documentation skips explaining this and I’m unclear about the following in this single, compound statement:
- the
=>expression inside the method argument - the params
configNameandconfigSetteraren’t initialized prior to this statement anywhere (?) - the high level use case / purpose of this
Can someone here can help me understand it?
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Microsoft.WindowsAzure.CloudStorageAccount.SetConfigurationSettingPublisher(
(configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});
}
Thanks
Its just a lambda.
It defines an “anonymous” method and passes it as a delegate (reference to a function call) parameter.
“=>” separates the anonymous method parameters from the method body.
configName and configSetter are the parameters to the method, their type is inferred from the code accepting and defining the method, clever eh?
Here its being used just to keep the code terse, in that you could define a real method to do the same job, but this way the code is smaller and has one less named method.
[Edit]
And it seems to be used here to control how CloudStorageAccount reads it settings.
I.e. the lambda directs reading of configuration items to the RoleEnvironment class to read from the Azure role’s service configuration.. but it could be changed to read them from elsewhere.