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

I'm trying to do

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

but does not work.

It's impossible?

+3
source share
3 answers

No, from the documentation:

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

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

+9
source

To continue John's answer, do this instead:

string username;
if (myDic.TryGetValue("username", out username))
{
  user.Username = username;
}
+6
source

VB, #.

VB ( ), out, .

, VB , . , .

#, , . , , "" , , "ref" .

, "out" . , , , , , VB, , . , . , ( ), , , .

, VB, , . , , , .

, :

class C
{
    private int m_bar;

    public int Bar { get { return m_bar; } set { m_bar = value; }}

    void foo(out int x)
    {
        x = 2;
        Console.WriteLine(Bar);
    }

    void DoStuff()
    {
        foo(out m_bar); //outputs 2
        Bar = 0;
        //pretend this works
        foo(out Bar); //outputs 0
        Console.WriteLine(Bar); // outputs 2
    }
}

DoStuff() foo, foo, , .

# .

, ( , , ).

+3

Source: https://habr.com/ru/post/1723925/


All Articles