简体   繁体   中英

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. The getter creates a new instance every time it's invoked. 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.

The property name is "DefaultSettings". Whenever the 'get' is called, it will create a new MySettings object.

MySettings settings = MySettings.DefaultSettings;

Here the "settings" local variable will have a my number of "0" and empty string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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