开发者

Understanding C# property syntax and getters/setters

I am coming over from the Java world and this construct is driving me nuts:

开发者_开发问答    private static string _muiUrl;

    private static string MUIUrl
    {
        get
        {
            if (String.IsNullOrEmpty(_muiUrl))
            {
                using (var db = new IntLMPDB())
                {
                    _muiUrl =
                        (from c in db.Control where c.ControlKey == "MUI_Url" select c.ControlValue).FirstOrDefault();
                }
            }
            return _muiUrl;
        }
    }

When I see that in a class and I want to use that property in the class itself, how should I call it?


Simply as follows:

var result = TypeWithMUIURLProperty.MUIUrl;

You can omit the class name as mentioned by others, however, for explicitness I will leave it in this example.


Inside the class, you do not need to qualify the property name at all, i.e. you write just

string url = MUIUrl;

Had the property been something “better” than just private, to access the property from a completely different class, you would need to qualify it using the class name (but you are allowed to do even inside the class, it is just not required), i.e.

string url = ThatClassYouDidNotName.MUIUrl;

(Side remark: this variant of lazy-initialization is not generally thread-safe.)


From within the class, just writing MUIUrl should do fine. That would pull an initialized value, and force the lazy instantiation.

If it were public and you were accessing it from somewhere else you would need to write YourClassName.MUIUrl. But since it is private, this is unnecessary.

Also, simply using MUIUrl will work from within the instance or other static members.


When I implement a self loading property that has a private backing store I follow the convention of never interact with the backing store, always read from the property.

I find it stupid that you need the private field, I feel this type of code should be inherit inside of {get; set;} that you should have implicit access to a field of the type of the property whether you use it directly, or fall back to the default auto-property wiring.


if the construct is driving you nuts, here's some sort of explanation as comment

private static string _muiUrl;
public static string MUIUrl
{
    get
    {
        //if the variable is null or empty
        if (String.IsNullOrEmpty(_muiUrl))
        {
            //using defines a scope, outside of which an object or objects will be disposed.
            //http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx
            using (var db = new IntLMPDB())
            {
                //this is linq
                //a simple example will be 
                //http://stackoverflow.com/questions/214500/which-linq-syntax-do-you-prefer-fluent-or-query-expression
                _muiUrl = (from c in db.Control 
                            where c.ControlKey == "MUI_Url" 
                            select c.ControlValue
                          ).FirstOrDefault();
                //or another linq syntax will be
                //_muiUrl= db.Control
                //        .Where(c => c.ControlKey == "MUI_Url")
                //        .FirstOrDefault()
                //        .ControlValue;
            }
        }
        return _muiUrl;
    }
}

Have deliberately given the property's access modifier as public since its how you call it outside class(feel free to make it private if its so).

Now call it as,

ClassName.MUIUrl
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜