Tutorial :C# syntax - Colon after a variable name



Question:

Quick question; I've recently upgraded to VS2010, and got the new version of ReSharper.

Now, when ReSharper is giving me autocomplete options for a variable, it give me the option of <variableName>:

What does the : stand for?

For example; I have this:

var productIds = new List<int>(inventoryItemsToProcess.Keys);  

And when I start typing out a line like this:

var lastOrderDates = GetProductLastOrderDates(pro  

It gives me the option for productIds as well as productIds:

What's the difference between the two?


Solution:1

The second is for C# 4.0 named arguments. And here's a link on MSDN.


Solution:2

The colon is necessary to indicate parameters. In C# 4.0, you can re-order and name your parameters, optionally, but the variable name must match the prototype and have the colon postfix.

public void Test(string something1, string something2)  {  }  

can be called as:

Test(something2: "bar", something1: "foo");  

if you want


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »