Wednesday 9 November 2011

Beware of recursion on getter/setter methods

Question Received:
      The Application is not responding apparently.

Solution:
     Check the code for any defects like recursion in the statement and methods.
     Defect I have found from the getter/setter methods.
     private string _mail;
     public string Mail
     {
          get
          {
               return _mail;
          }
          set
          {
              if(condition)
              {
                   Mail="mail@daemon.com";
              }
          }
     }
     public DataSource()
     {
          Mail="mail@daemon.com";
     }

[Note:]
The culprit is,
       In setter method, Mail="mail@daemon.com" makes it call the setter method recursively.

No comments:

Post a Comment