get and set in c#
<p>I'm new to c# syntax. I wrote the following class (the codebehind for the masterpage of my website):</p>

<pre><code>public partial class MasterPagePro : System.Web.UI.MasterPage
{
    public String pageTitle
    {
        get
        {
            if (this.pageTitle == null)
                return "";
            else
                return " - " + this.pageTitle;
        }
    }

}
</code></pre>

<p>However, when I try to access pageTitle like this in the html:</p>

<pre><code>&lt;title&gt;MySite&lt;%=pageTitle%&gt;&lt;/title&gt;
</code></pre>

<p>I get a stackoverflow error. Looking at the code, it's pretty clear that the problem is that the method is calling itself recursively, but I don't know what to write to resolve this. I could do something like:</p>

<pre><code>public partial class MasterPagePro : System.Web.UI.MasterPage
{
    private String _pageTitle

    public String pageTitle
    {
        get
        {
            if (_pageTitle == null)
                return "";
            else
                return " - " + _pageTitle;
        }
        set { _pageTitle = value; }
    }
}
</code></pre>

<p>But that seems to defeat the point of having the syntactic shortcut at all. What is the right way to do this?</p>
