开发者

create property for encapsulation

开发者 https://www.devze.com 2023-02-13 19:22 出处:网络
when for Perperty created a private field,Do it is compulsor?? and when do not created? enter code here

when for Perperty created a private field,Do it is compulsor??

and when do not created?

enter code here 

namespace ApplicationStartSample
{
public class Configuration
{
    private Configuration()
    {
    }

    private static Configuration _Current;
    public static Configuration Current
    {
        get
        {
            if (_Current == null)
                _Current = new Configuration();

            return _Current;
        }
    }

    private const string Path = "Software\\MFT\\Registry Sample";

    public bool EnableWelcomeMessage
    {
        get
        {
            return b开发者_如何学Cool.Parse(Read("EnableWelcomeMessage", "false"));
        }
        set
        {
            Write("EnableWelcomeMessage", value.ToString());
        }
    }

    public string Company                      //why do not create private field?
    {
        get
        {
            return Read("Company", "MFT");
        }
        set
        {
            Write("Company", value);
        }
    }

    public string WelcomeMessage
    {
        get
        {
            return Read("WelcomeMessage", string.Empty);
        }
        set
        {
            Write("WelcomeMessage", value);
        }
    }

    public string Server
    {
        get
        {
            return Read("Server", ".\\Sqldeveloper");
        }
        set
        {
            Write("Server", value);
        }
    }

    public string Database
    {
        get
        {
            return Read("Database", "Shop2");
        }
        set
        {
            Write("Database", value);
        }
    }

  private static string Read(string name, string @default)
  {
  RegistryKey key = Registry.CurrentUser.OpenSubKey(Path, false);

  if (key == null)
    return @default;

 try
 {
    string result = key.GetValue(name).ToString();
    key.Close();

    return result;
}
catch
{
    return @default;
}
}

  private static void Write(string name, string value)
 {
 try
{
    RegistryKey key = Registry.CurrentUser.OpenSubKey(Path, true);

    if (key == null)
        key = Registry.CurrentUser.CreateSubKey(Path);

    key.SetValue(name, value);
    key.Close();
}
catch
{
}
}
}
}


If you're asking if you can eliminate the private field for your Current property, you could do this (though it would no longer initialise Configuration lazily):

public class Configuration
{
    static Configuration()
    {
        Current = new Configuration();
    }

    public static Configuration Current { get; private set; }
}

Note: This is an Auto-Implemented Property and requires C# 3.0.

You could also use a public field instead (though if you ever need to change this to a property, you would need to recompile anything that's calling it):

public class Configuration
{
    public static Configuration Current = new Configuration();
}
0

精彩评论

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

关注公众号