简体   繁体   English

了解C#属性的语法和getter / setter

[英]Understanding C# property syntax and getters/setters

I am coming over from the Java world and this construct is driving me nuts: 我来自Java世界,这种构造使我发疯:

    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, ie 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), ie 如果该属性比private属性更“好”,则可以从完全不同的类访问该属性,则需要使用类名称对其进行限定(但是即使在该类内部也可以使用它,但这不是必需的) ,即

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. 从班级内部,只需编写MUIUrl就可以了。 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 . 如果它是公共的,并且您正在其他地方访问它,则需要编写YourClassName.MUIUrl But since it is private, this is unnecessary. 但是由于它是私有的,所以这是不必要的。

Also, simply using MUIUrl will work from within the instance or other static members. 同样,只需使用MUIUrl就可以在实例或其他静态成员中使用。

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;} 我觉得您需要私有字段很愚蠢,我觉得这种类型的代码应该继承{get; set;} {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. {get; set;} ,则无论您是直接使用属性还是使用默认的自动属性连线,都应该隐式访问属性类型的字段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM