c# constructor calling toString : <p>I have an object that overrides iComparable and that also overrides toString().</p>

<p>When the constructor is called it is also calling toString(), but not hitting any break points in the toString() method.</p>

<p>The toString() is also called when each line of the constructor is executed, and as soon as the DataSource property is updated it gets the data from the database and puts in unexpected results.</p>

<p>I have proven this with the logging of environment.stacktrace for each call.</p>

<p>Is this expected behaviour, and is there anyway to only call the toString() when I explicitly call it rather than automatically.</p>

<p>Code for the class is below</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Business.Data;

namespace PfWToEpayfactGIF
{
class PsPayChangeDetails : IComparable&lt;PsPayChangeDetails&gt;
{
  public DateTime EffectiveDate { get; set; }
  public String Code { get; set; }
  public string ChangeType { get; set; }

  public int DataSourceX { get; set; }

  private string gradeID;
  private string gradeSubCode;
  private string gradePercentage;
  private string IDField;
  private string salary;
  private string maxOTRateType;
  private string lWeightType;
  private string onTempPromotion;
  private string RTIHoursInd;
  private string paygroupID;
  private string classification;
  private string hoursPayable;
  private string workingPatternID;
  private string OSPSchemeNo;
  private string milestoneDate;
  private string employeeNo;


  public PsPayChangeDetails(string code, DateTime effectiveDate, string changeType, int groupSource)
  {
     EffectiveDate = effectiveDate;
     Code = code;
     ChangeType = changeType;
    DataSourceX = groupSource;
  }

  public void ReadLine(string[] line)
  {
     employeeNo = line[0] != string.Empty ? line[0] : employeeNo;
     try
     {
        if (Code == "E420")
        {
           gradeID = line[4] != string.Empty ? line[4] : gradeID;
           gradeSubCode = line[5] != string.Empty ? line[5] : gradeSubCode;
           gradePercentage = line[6] != string.Empty ? line[6] : gradePercentage;
           IDField = line[7] != string.Empty ? line[7] : IDField;
           salary = line[8] != string.Empty ? line[8] : salary;
           maxOTRateType = line[9] != string.Empty ? line[9] : maxOTRateType;
           lWeightType = line[10] != string.Empty ? line[10] : lWeightType;
           onTempPromotion = line[11] != string.Empty ? line[11] : onTempPromotion;
           RTIHoursInd = line[12] != string.Empty ? line[12] : RTIHoursInd;
        }
        else
        {

           paygroupID = line[4] != string.Empty ? line[4] : paygroupID;
           classification = line[5] != string.Empty ? line[5] : classification;
           hoursPayable = line[6] != string.Empty ? line[6] : hoursPayable;
           workingPatternID = line[7] != string.Empty ? line[7] : workingPatternID;
           OSPSchemeNo = line[8] != string.Empty ? line[8] : OSPSchemeNo;
           milestoneDate = line[9] != string.Empty ? line[9] : milestoneDate;
        }
     }
     catch (IndexOutOfRangeException)
     {
        //ignore the exception as its telling us we dont have all the fields which is fine.
     }

  }

  public override string ToString()
  {
     using (System.IO.StreamWriter write = new System.IO.StreamWriter(@"c:\temp\test.txt", true))
     {
        write.WriteLine(Environment.StackTrace);
     }
        string output = $"{employeeNo},{Code},{EffectiveDate.ToString("dd/MMM/yyyy")},{ChangeType},";
     if (Code == "E420")
     {
        output += $"{gradeID},{gradeSubCode},{gradePercentage},{IDField},{salary},{maxOTRateType},{lWeightType},{onTempPromotion},{RTIHoursInd}";
     }
     else
     {
        using (DataEntities db = new DataEntities(DataSourceX))
        {

           if (paygroupID == null)
           {
              string partTime = workingPatternID == "PT" ? "Y" : "N";
              paygroupID = db.PayGroupEESetting.Where(pge =&gt; pge.PartTimeInd == partTime &amp;&amp; pge.EnteredDate == db.PayGroupEESetting.Where(pg =&gt; pg.PayGroup == pge.PayGroup &amp;&amp; pg.EnteredDate &lt;= EffectiveDate).Max(pg =&gt; pg.EnteredDate)).OrderBy(pge =&gt; pge.PayGroup).FirstOrDefault().PayGroup;
           }
           workingPatternID = workingPatternID == null ? "FT" : "PT";
           if (OSPSchemeNo == null)
           {
              OSPSchemeNo = db.OSPScheme.Min(o =&gt; o.SchemeNo).ToString();
           }
        }
        output += $"{paygroupID},{classification},{hoursPayable},{workingPatternID},{OSPSchemeNo},{milestoneDate}";
     }
     return output;
  }


  public int CompareTo(PsPayChangeDetails other)
  {
     if (this.Code == other.Code)
     {
        return this.EffectiveDate.CompareTo(other.EffectiveDate);
     }
     else
     {
        return this.Code.CompareTo(other.Code);
     }
  }
}
}
</code></pre>

<p>Thanks for any help.</p>

<p>Ben</p>
