我不能使用TryGetValue并将值赋给属性?

mrb*_*lah 4 c# dictionary

我正在努力做到

myDic.TryGetValue("username",out user.Username);

但它不起作用.

这不可能吗?

Joh*_*nan 9

不,来自文档:

"属性不是变量,因此不能作为out参数传递."

http://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx


G-W*_*Wiz 6

要继续约翰的回答,请改为:

string username;
if (myDic.TryGetValue("username", out username))
{
  user.Username = username;
}
Run Code Online (Sandbox Code Playgroud)