title: Dictionary TryGetValue
body: <p>I want to preserve state in windows phone when user leaving my app.<br>
  How can I create a general method <code>TryGetValue</code> that gets value from dictionary?</p>
  
  <p>My code so far:</p>
  
  <pre><code>    public class StatefulPhoneApplication : PhoneApplicationPage
      {
          #region constructor
          public StatefulPhoneApplication()
          {
              IsNewPageInstance = true;
          }
          #endregion
  
          #region properties
          protected bool IsNewPageInstance { get; private set; }
          protected bool IsStatePreserved
          {
              get
              {
                  if (this.State.ContainsKey("StatePreserved"))
                      return (bool)this.State["StatePreserved"];
                  else
                      return false;
              }
          }
          #endregion
  
          #region preservation methods
          protected void PreserveControlState(Control control)
          {
              if (control is TextBox)
                  PreserveTextBoxState(control as TextBox);
              else
                  PreserveCheckBoxState(control as CheckBox);
  
              this.State["StatePreserved"] = true;
          }
  
          protected void PreserveTextBoxState(TextBox textBox)
          {
              this.State[textBox.Name + ".Text"] = textBox.Text;
              this.State[textBox.Name + ".SelectionStart"] = textBox.SelectionStart;
              this.State[textBox.Name + ".SelectionLength"] = textBox.SelectionLength;
          }
  
          protected void PreserveCheckBoxState(CheckBox checkBox)
          {
              this.State[checkBox.Name + ".IsChecked"] = checkBox.IsChecked;
          }
  
          protected void PreserveFocusState(FrameworkElement parent)
          {
              Control focusedControl = FocusManager.GetFocusedElement() as Control;
  
              if (focusedControl == null)
              {
                  this.State["FocusedControlName"] = null;
              }
              else
              {
                  Control controlWithFocus = parent.FindName(focusedControl.Name) as Control;
  
                  if (controlWithFocus == null)
                  {
                      this.State["FocusedElementName"] = null;
                  }
                  else
                  {
                      this.State["FocusedElementName"] = focusedControl.Name;
                  }
              }   
  
          }
          #endregion
  
          #region restoration methods
          private void RestoreControlState(Control control)
          {
              if (control is TextBox)
                  RestoreTextBoxState(control as TextBox, string.Empty);
              else if (control is CheckBox)
                  RestoreCheckBoxState(control as CheckBox, false);
          }
          #endregion
      }
  </code></pre>
  
  <p>I want to call just like this.</p>
  
  <pre><code>    private void RestoreTextBoxState(TextBox textBox, string defaultValue)
      {
          textBox.Text = TryGetValue&lt;string&gt;(textBox.Name + ".Text", defaultValue);
          textBox.SelectionStart = TryGetValue&lt;int&gt;(textBox.Name + ".SelectionStart", defaultValue);
          textBox.SelectionLength = TryGetValue&lt;int&gt;(textBox.Name + ".SelectionLength", defaultValue);
      }
  </code></pre>
  
  <p>I cannot find a good sample that works.</p>