简体   繁体   English

为什么属性类型名称与C#中的类名称相似?

[英]Why is the property type name similar to the class name in C#?

public class MySettings
{
    public int MyNumber { get; set; }
    public string MyString { get; set; }



    private static MySettings DefaultSettings
    {
        get
        {
            return new MySettings 
            {    
                MyNumber = 0,
                MyString = "", 
            };


        }
    }
}

You have a static property that returns an instance of type MySettings. 您有一个静态属性,该属性返回MySettings类型的实例。 The getter creates a new instance every time it's invoked. 每次调用时,getter都会创建一个新实例。 Essentially it looks like a convenience wrapper for creating objects. 本质上,它看起来像是用于创建对象的便捷包装。

MySetting is the return type, so the newed up object has to also be of that type. MySetting是返回类型,因此更新的对象也必须是该类型。

The property name is "DefaultSettings". 该属性名称为“ DefaultSettings”。 Whenever the 'get' is called, it will create a new MySettings object. 每当调用“ get”时,它将创建一个新的MySettings对象。

MySettings settings = MySettings.DefaultSettings;

Here the "settings" local variable will have a my number of "0" and empty string. 在这里,“设置”局部变量的my编号为“ 0”,并且为空字符串。

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

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