Graham
Graham

Reputation: 7802

How to pass multiple parameters as an array in a C# Azure Function?

I am porting one of our node.js Azure Functions to C#, so this is C# running in a .csx container. I'm running in the local Windows Function emulator and have also tried this in the Functions runtime in Azure, both show the same compiler errors.

I have seen multiple other questions asking something similar, but there seems to be something different about running in a .csx file or the functions runtime as none of the answers work.

This is the method signature that I'm calling :

DocumentClient.ExecuteStoredProcedureAsync<TValue> Method (Uri, Object[])

This is the working code from run.csx:

string param1 = "param1";
string param2 = "param2";

// object[] params = {"param1", "param2"};

var response = await client.ExecuteStoredProcedureAsync<object>(
  sprocUri,
  param1,
  param2
);

When I try to add the two params into an array and uncomment the line object[] params = {"param1", "param2"}; then I get this error from the compiler:

run.csx(72,14): error CS1001: Identifier expected
run.csx(72,14): error CS1003: Syntax error, ',' expected
run.csx(72,21): error CS1002: ; expected
run.csx(72,21): error CS1525: Invalid expression term '='
run.csx(72,23): error CS1525: Invalid expression term '{'
run.csx(72,23): error CS1002: ; expected
run.csx(72,32): error CS1002: ; expected
run.csx(72,32): error CS1513: } expected
run.csx(72,42): error CS1002: ; expected
Compilation failed.

(Line 72 corresponds to the array assignment statement)

I have tried all of these different ways to define the array, but I get similar errors for each:

Object[] params = {"param1", "param2"};
object[] params = new[] {"param1", "param2"};
object[] params = new[] {param1, param2};
string[] params = {"param1", "param2"};
string[] params = new string[] {"param1", "param2"};
dynamic[] params = new[] {"param1", "param2"};

Is there something different about Azure Functions where it needs a different style of array definition? There is nothing mentioned about arrays in the C# Developer Reference.

Upvotes: 1

Views: 2777

Answers (1)

Zdeněk Jel&#237;nek
Zdeněk Jel&#237;nek

Reputation: 2923

The issue you are facing is that you named the variable params which is a reserved word in C# (see params (C# reference)). Try using a different name.

Should you be really set on using params, you could possibly use a @ prefix which can be used in C# to create an identifier which would be a reserved word otherwise:

object[] @params = { param1, param2 };

Upvotes: 2

Related Questions