开发者

get and set in c#

I'm new to c# syntax. I wrote the following class (the codebehind for the masterpage of my website):

public partial class MasterPagePro : System.Web.UI.MasterPage
{
    public String pageTitle
    {
        get
        {
            if (this开发者_运维百科.pageTitle == null)
                return "";
            else
                return " - " + this.pageTitle;
        }
    }

}

However, when I try to access pageTitle like this in the html:

<title>MySite<%=pageTitle%></title>

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:

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; }
    }
}

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


In your first example, you're expecting the compiler to know whether or not you actually meant to recursively call the same property. Since the compiler can't know your intentions, it will create the code as written, which as you pointed out, leads to a stackoverflow.

The syntatic shortcut is for when you have a simple property with a assigning/returning setter/getter. The compiler creates the backing class member for you, and generates the get/set routines.

For example:

class foo {

 public int myInt;

 public int MyInt { get { return myInt;} 
                    set { myInt = value;} }

 }

vs

 class foo {

 public int MyInt { get; set; }

 }

In this case, it is not ambiguous at all what you are trying to do, thus the compiler can help out and auto generate as necessary.

If you need to implement custom setter/getter functionality, that also includes setting a value, you must make use of a backing class member, as you have shown in your 2nd example.


When you use a getter in this way you also need to manually add a field like you have in your second example.

You can't just make reference to the same property as in your first example.

You mention a syntactic shortcut but you're not using a shortcut as far as I can tell.

The shortcut for properties when no processing is done on the variable before it's returned would be:

public string pageTitle{get;set;}

This automatically creates the backing field for you.


The second block of code you provided is as close to the "correct" way to implement non-trivial (with logic) properties in c# as I'm aware of.


Also, properties (get and set syntax) usually have PascalCase, i.e. PageTitle.


  • List item

You are returning the property, which will walk into the get again. Does this property have a backing field?

Your proposed way is correct. The idea of properties is only to be syntactic sugar over get and set methods, not around removing the need for a local field. Auto-properties is syntactic sugar for that, but you cannot provide an implementation in that case.

What you might be thinking of is an auto-property:

public string MyName { get; set; }

But for these you cannot implement get or set.

Also, if you are providing a non-trivial implementation for a property, note that there are a couple of guidelines:

  • The get and set actions should be lightweight, ie fast.
  • The return value of the property should not change between calls unless it is set to a different value (ie, if the state of the class internally causes a different value, it's advised to use a method instead to return the value).


The "syntactic shortcut" is not necessarily there to be a syntactic shortcut. If you want to use auto-properties, you can use

public String PageTitle { get; set; }

and everything is there for you, including a backing field. The real point of properties is to hide the backing field from implementers... OOP, abstraction and inheritence and all that stuff. With a property, you can add validation or change the format of how it gets returned without having to search for every instance of access to the backing field.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜