-1

What does it mean to initialise a parameter inside a declaration?

I was surprised to see this code recently. When will quoteId be -1 ? All the time? if it is passed in as null?, if it is omitted ?

    [HttpGet]
    [ActionName("GetBillingInfo")]
    public BillingInfo GetBillingInfo(string kValue = "", int quoteID = -1)
    {
        return _Repository.GetBillingDetails(kValue, quoteID);
    }
}

[Update]

The answer is that assigning the parameter a value causes the parameter to be optional.

This question is the reverse of asking how to create an optional parameter.

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • reading https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments – Kirsten Jan 13 '18 at 21:13
  • 3
    Possible duplicate of [How can you use optional parameters in C#?](https://stackoverflow.com/questions/199761/how-can-you-use-optional-parameters-in-c) – Progman Jan 13 '18 at 21:14
  • 1
    Please note that the value provided for the optional parameter will be compiled into the calling site when a call is made. In other words, if a dll is compiled with a call to `GetBillingInfo`, where the source code that included that call did not provide `quoteID`, the value `-1` will be passed in, even if the original source code is changed to use a different value. The default value is compiled into the assembly of the calling site at compile time. If the default value changes later you will need to recompile projects that call the method to get the new default value. – Lasse V. Karlsen Jan 13 '18 at 21:40
  • What Lasse Vågsæther Karlsen wrote in his comment pretty much answers the question. Also, it's the main reason I prefer method ovrrloading instead of optional parameters - since if the default value changes, you don't need to worry about compiling everything that use you class. – Zohar Peled Jan 13 '18 at 21:46

4 Answers4

2

The concept is called "default values" and allows for a different concept, "optional parameters".

Simply put, if you call the method and doesn't supply the specified parameter, its default value will be used.

But what does it mean?

There's two ways this concept could be interpreted by the compiler:

  1. Calls to this method that doesn't supply this parameter will end up using the default value compiled into the method being called.
  2. Calls to this method that doesn't supply this parameter will pass in the default value that was declared, at the time of compiling the code that calls it.

So which is it?

Let's look at two separate pieces of code:

public void Test(int value = 42)
{
}

and

public void SomeOtherTest()
{
    SomeInstance.Test(); // this is the same Test as above
}

Let's assume these two are in different projects, and will thus be compiled into different assemblies, dll's.

What happens at compile time for that second piece of code is that the compiler sees that the default value for the missing parameter, value, is 42, and thus adds this to the compiled code.

In other words, the second piece of code is treated as though it was written like this:

public void SomeOtherTest()
{
    SomeInstance.Test(42); // this is the same Test as above
}

Note the additional 42 in the parameter call here.

So the syntax in the question means two things:

  1. A parameter with a default value can be ommitted in calls to the method
  2. Such calls, where the parameter is ommitted, will be compiled as though the parameter value was there all along, with the default value for that parameter.

Why is this important?

Well, consider if the two pieces of code above was present in two different projects, and thus compiled into two different assemblies, dll's.

If you later change the source code of the project that declares that method, to provide a different default value, the other project is still compiled to supply 42.

Unless the other assembly, where the call is being made, is recompiled, the value 42 will still be passed to the method.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
1

This is called optional parameter. The value of quoteID can be either -1 if you don't specify the argument during function call else it will have non nullable value. If you pass null then compiler shall give you an error. If you wan't quoteID to hold null value then your function signature should be like below

    [HttpGet]
[ActionName("GetBillingInfo")]
public BillingInfo GetBillingInfo(string kValue = "", int? quoteID = -1)
{
    return _Repository.GetBillingDetails(kValue, quoteID);
}
Maddy
  • 774
  • 5
  • 14
0

This is called optional parameters. it simply means if the caller of this method does not supply values for those parameters then the default value provided to the parameters will be used.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

Its Optional Parameter, If you pass value to int quoteID , it will hold that value , If not , it will be -1 that is default value.

You will be able to call your method by 4 ways

GetBillingInfo();//No parametes (kValue,quoteID)
GetBillingInfo("kvalue"); // KValue
GetBillingInfo("KValue" , 20); // kvalue + quoteID

You can call it with paramName (Named Parameters)

 GetBillingInfo(quoteID: 20) //quoteID

Update Question : how to create an optional parameter

Answer is in question

Syntax is

MethodName(DataType Param = <DefaultValue> , DataType Param2 = <DefaultValue>)

GetBillingInfo(string kValue = "", int quoteID = -1)

Read Named and Optional Arguments (C# Programming Guide)

Ehsan Ullah Nazir
  • 1,827
  • 1
  • 14
  • 20