Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment operator (=) in argument of Method Definition

Tags:

c#

.net

See below method definition.

What is it called in C# where the equals sign is in method parameter.

Does it default method parameter initialization??

public List<Iabc> MyMethod(out List<Ixyz> faces, Type typeXYZ = null, int flag = -1)
{
    //...
    //...   
}

NOTE: Here Iabc and Ixyz are any Interfaces.

like image 961
Pritesh Avatar asked May 18 '11 08:05

Pritesh


People also ask

What is assignment operator in C#?

The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand.

What is parameter and argument in Python?

A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that are sent to the function when it is called.

What does += mean in C#?

+= Operator. += operator adds the value of the variable on the left with the value on the right, which is (result) then assigned to the variable that is on the left. -= Operator.


3 Answers

They're called optional (or named) arguments. MSDN usually has these things explained pretty well:

Named and Optional Arguments (C# Programming Guide)

like image 61
Matti Virkkunen Avatar answered Sep 19 '22 13:09

Matti Virkkunen


When using named arguments, be aware that changing argument names will break code. (where named parameters are used)

Also, remember that the default value is actually stored in the call site meaning that if you at some later point change the default value, code that is calling the method and was compiled before the change, will still use the old value. it might not matter in all situations but its something to be aware of.

like image 32
aL3891 Avatar answered Sep 19 '22 13:09

aL3891


That's an optional argument in C# 4.0

like image 45
devdigital Avatar answered Sep 19 '22 13:09

devdigital