为什么在Visual Studio自动完成列表中有与":"相同的变量?

Xaq*_*ron 6 c# autocomplete visual-studio

在使用函数时,VS自动完成列表之间myVar和之间的区别是什么myVar:.为什么第二个被添加到此列表中?

Fré*_*idi 20

C#4.0引入了命名参数.此功能允许您通过名称而不是位置来标识方法参数:

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);
Run Code Online (Sandbox Code Playgroud)

Intellisense已经更新以支持该功能,这就是为什么当从当前作用域访问的符号与方法参数同名时,其自动完成机制现在提供两种选择.


Jon*_*ter 6

这可能是在调用方法时为参数设置值时,是吗?在C#.NET 4中,您可以在调用方法时设置命名参数.这样就无需必须按设定顺序输入参数.

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);
Run Code Online (Sandbox Code Playgroud)