What's the difference between myVar
and myVar:
in VS auto-complete list while working with functions. Why the second one is added to this list ?
C# 4.0 introduced named arguments . This feature allows you to identify 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 that feature, that's why its autocompletion mechanism now offers both choices when a symbol accessible from the current scope has the same name as a method argument.
This is probably when you're setting a value for a parameter when calling a method, yeah? In C# .NET 4, you can set named parameters when calling a method. This removes the need for having to enter your parameters in a set order.
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);
this is a very cool feature. it allows your code to be more tolerant to parameter ordering changes...
In addition to what the others wrote: The first one is a (local) variable or field, while the last one is the name of the parameter 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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.