Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't I use TryGetValue and assign the value to a property?

Tags:

c#

dictionary

I'm trying to do

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

but its not working.

is this not possible?

like image 946
mrblah Avatar asked Dec 03 '22 06:12

mrblah


2 Answers

No, from the documentation:

"Properties are not variables and therefore cannot be passed as out parameters."

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

like image 115
John Buchanan Avatar answered May 09 '23 03:05

John Buchanan


To continue John's answer, do this instead:

string username;
if (myDic.TryGetValue("username", out username))
{
  user.Username = username;
}
like image 22
G-Wiz Avatar answered May 09 '23 03:05

G-Wiz