我应该使用静态方法还是非静态方法? [英] Should I go with static methods or non static methods?

查看:119
本文介绍了我应该使用静态方法还是非静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C#创建了一个控制台应用程序,并且有一个 main 方法(静态),我的要求是初始化2个计时器并分别处理2个方法,这些方法会定期调用以完成某些任务.现在,我采用了所有其他静态方法/变量,因为它们是从计时器处理程序事件中调用的(由于从main调用它是静态的).

现在,对于上述情况,我想知道如果此控制台长时间运行,将如何消耗内存?如果我想应用oops概念,那么我是否需要使所有方法/变量都是非静态的,并通过创建类的对象进行访问?在这种情况下,如何消耗内存?

更新: 以下是我的代码段

 public class Program
    {
        readonly static Timer timer = new Timer();
        static DateTime currentDateTime;
        //other static variables
        //-----
        static void Main()
        {
            timer.Interval = 1000 * 5;
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();

            //2nd timer
            //-----

            System.Console.ReadKey();
            timer.Stop();
        }

        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            currentDateTime = DateTime.UtcNow;
            PushData();

        }

        private static void PushData()
        {
            //Code to push data
        }
    }

解决方案

您不应该基于内存消耗(可能不会有太大变化)来决定是否使用静态字段/方法.相反,您应该选择产生更干净,更可测试的代码的地方.

如果不需要任何多态行为,并且该方法在逻辑上不作用于该类型的实例,则

静态方法也可以(IMO).但是,如果您还 涉及到静态变量,那将是一个更大的问题.除了常量之外,静态变量会使代码更难于在多个线程中进行测试,重用和正确处理.

听起来您可能应该使用实例变量和方法.只要使您的Main方法创建该类的实例,它就可以使用该实例创建委托以传递给计时器.在不知道自己在做什么的情况下很难做到比这更精确,但这听起来好像您是在使用静态函数来立即获得便利,而不是因为这样做是对的,这总是令人担忧.

I have created a console application in C# and there is main method (static) and my requirement is to initialize 2 timers and handles 2 methods respectively which will be called periodically to do some task. Now I have taken all other methods/variables static because that are calling from timer handler events (which are static due to calling it from main).

Now i would like to know for above scenario how memory is going to be consumed if this console running for long time? if i would like to apply oops concept then do i need make all methods/variables non static and access that by creating object of class? in this case how memory going to be consume?

Update: Following is snippet of my code

 public class Program
    {
        readonly static Timer timer = new Timer();
        static DateTime currentDateTime;
        //other static variables
        //-----
        static void Main()
        {
            timer.Interval = 1000 * 5;
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();

            //2nd timer
            //-----

            System.Console.ReadKey();
            timer.Stop();
        }

        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            currentDateTime = DateTime.UtcNow;
            PushData();

        }

        private static void PushData()
        {
            //Code to push data
        }
    }

解决方案

You shouldn't be deciding whether or not to use static fields/methods based on memory consumption (which likely won't be altered much). Instead, you should go with what produces cleaner, more testable code.

Static methods are okay (IMO) if you don't need any kind of polymorphic behaviour, and if the method doesn't logically act on an instance of the type. However, if you've also got static variables involved, that's more of an issue. Static variables - other than constants - can make code much harder to test, reuse, and handle correctly in multiple threads.

It sounds like you probably should be using instance variables and methods. Just make your Main method create an instance of the class, and it can use that instance to create delegates to pass to the timer. It's hard to be much more precise than that without knowing more about what you're doing, but it does sound like you're using statics for immediate convenience rather than because it's the right thing to do, which is always a worry.

这篇关于我应该使用静态方法还是非静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆