Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:41:54+00:00 2026-05-20T08:41:54+00:00

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

  • 0

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
{
}
}
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:41:55+00:00Added an answer on May 20, 2026 at 8:41 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

IS it possilbe to inline code a porperty like Font-bold of control like linkbutton?
Let's say I've a class myClass which has few properties, such as property1 ,
Here is part of a stack-trace from a recent run of an unreliable application
i have one interface /// <summary> /// Summary description for IBindable /// </summary> public
I am setting peroperty Margin and Padding of a window and it doesn't take
Suppose I have an Entity Order with OrderDetails as child preperty. I enable lazyloading
Some Context From Javascript: The Definitive Guide : When regexp is a global regular
HI, How do I move (drag) a Grid Panel inside a WPF Window? The
does anybody know when the columns are generated of a GridView? After what event?
I built a wrapper around NpgSQL for a bunch of the methods I usually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.