C# - WPF App static - class or only function

Markus Freitag 3,786 Reputation points
2020-08-04T14:00:49.227+00:00

Hello,

a)

 public static class Logger
    {
        public static void Write(string message) => Log.Write(message);
        public static void Close() => Log.Close();

        private static LogIntern Log = new LogIntern();

        public static int CheckIntervalHours { get; set; } = 1;
        public static string LogDirPath = "log";

        private class LogIntern

b)

public partial class MessageBoxYes_No : Window, ICommand
    {
        public string Message { get; set; }
        public string Header { get; set; }
        private string Result;

        public event EventHandler CanExecuteChanged;
        public bool CanExecute(object parameter) => true;
        //public void Execute(object parameter) => ExitMessageBox((parameter != null) ? parameter.ToString() : string.Empty);
        public void Execute(object parameter)
        {
            ExitMessageBox((parameter != null) ? parameter.ToString() : string.Empty);
        }

        public MessageBoxYes_No()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public static string ShowDialog(string msg, string questionHeader = "Information...")
        {
            var msgBox = new MessageBoxYes_No() { Message = msg, Header = questionHeader };
            msgBox.ShowDialog();
            return msgBox.Result;
        }

What are the reasons, what are the advantages if I define a class static or just a single function.

static if I want to have a global single instance for my application. SingleTon Pattern.

What is your experience, what is best practice?

The easiest way is to declare everything static, like in my example Logger.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    2020-08-05T07:23:13.977+00:00

    The main difference between static class/member and non-static class/member is that a static class cannot be instantiated. So we access the members of a static class by using the class name itself. The Singleton is a non-static class that allows only one instance of itself to be created,so it can implement interfaces or derive from useful base classes.

    In the using of memory, both static and non-static methods are resident in memory after the first load, and the difference is that non-static methods are reclaimed by GC.

    Singleton's two main advantages:(Derive from Implementing Singleton in C#)

    Because the instance is created inside the Instance property method, the class can exercise additional functionality (for example, instantiating a subclass), even though it may introduce unwelcome dependencies.

    The instantiation is not performed until an object asks for an instance; this approach is referred to as lazy instantiation. Lazy instantiation avoids instantiating unnecessary singletons when the application starts.

    In general, singleton has more advantages than static class, use singleton mode if you're good at singletons, and use static class if you're just using a simple tool class.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.