Why do I have the same variables with ":" in the full list of Visual Studio auto-recordings?

What is the difference between myVar and myVar: in VS auto-complete list when working with functions. Why is the second added to this list?

+6
c # autocomplete visual-studio
source share
4 answers

C # 4.0 introduced named arguments . This function allows you to define method arguments by their names instead of their position:

 public void Foo(int bar, string quux) { } // Before C# 4.0: Foo(42, "text"); // After C# 4.0: Foo(bar: 42, quux: "text"); // Or, equivalently: Foo(quux: "text", bar: 42); 

Intellisense has been updated to support this feature, so its autocomplete mechanism now offers both options when a character accessible from the current scope has the same name as the method argument.

+20
source share

This is probably when you set a parameter value when calling a method, yes? In C # .NET 4, you can set named parameters when calling a method. This eliminates the need to enter your parameters in the prescribed manner.

 private void MyMethod(int width, int height){ // do stuff } //These are all the same: MyMethod(10,12); MyMethod(width: 10, height: 12); MyMethod(heigh: 12, width: 12); 
+6
source share

This is a very cool feature. this allows your code to be more tolerant of parameter order changes ...

+2
source share

In addition to what others wrote: The first is a (local) variable or field, and the latter is the parameter name of the called method. In code:

 private void MyFirstMethod(int myVar) { Console.WriteLine(myVar); } private void MySecondMethod(int myVar) { MyFirstMethod(myVar); // Call with the value of the parameter myVar MyFirstMethod(myVar: myVar); // Same as before, but explicitly naming the parameter MyFirstMethod(5); // Call with the value 5 MyFirstMethod(myVar: 5); // Same as before, but explicitly naming the parameter } 
+2
source share

All Articles