创建封装属性

发布于 2024-10-19 08:16:27 字数 2162 浏览 3 评论 0原文

当Perperty创建了一个私有字段时,它是强制性的吗?

什么时候不创建?

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 bool.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
{
}
}
}
}

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 bool.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
{
}
}
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-10-26 08:16:27

如果您询问是否可以消除 Current 属性的私有字段,您可以这样做(尽管它不再延迟初始化 Configuration):

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

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

注意:这是自动实现的属性并且需要 C# 3.0。

您也可以使用公共字段(尽管如果您需要将其更改为属性,则需要重新编译调用它的任何内容):

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文