In C#, the var keyword is used to declare a variable with implicit typing. When you use var, the compiler infers the type of the variable based on the type of the expression on the right-hand side of the assignment.
In your code, you have declared bodyObj as an anonymous object with an empty initializer. Anonymous objects are read-only, meaning you cannot modify their properties once they are created.
To add a property to your anonymous object, you can modify your code to use an object initializer like this:
public async Task<MyObject> ManageObjectAsync(string actionparam, int objectid)
{
var bodyObj = new { action = "enable" };
if (actionparam == "enable")
{
bodyObj = new { action = "enable", id = objectid };
}
// rest of your code
}
In the example above, we initialize bodyObj with an anonymous object that has a action property set to "enable". Then, inside the if block, we create a new anonymous object that has the action property set to "enable" and a new id property set to the value of objectid. We then assign this new object to bodyObj, effectively adding a new property to the anonymous object.
Note that this creates a new anonymous object each time you assign to bodyObj, so any previous properties that were set will be lost. If you need to add multiple properties, you can chain object initializers like this:
var bodyObj = new { action = "enable" };
if (actionparam == "enable")
{
bodyObj = new { action = "enable", id = objectid, name = "my object" };
}
n this example, we're adding two new properties to the bodyObj object, id and name.