C#中的控制台应用程序使用静态变量 [英] Console Application in C# using static variables

查看:119
本文介绍了C#中的控制台应用程序使用静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手来构建将继续在后台运行的控制台应用程序(在C#中)。我使用Visual Studio 2010生成模板,因此它生成:



命名空间MyNameSpace 
{
class Program
{
static int Main(string [] args)
{
}
}
}





该程序是一个TCP侦听器,所以我有一个连续的循环来监听TCP messeges。在没有偏离轨道的情况下,在类程序中,我在Main例程旁边有其他例程,一个AcceptCallBack,ReadCallback和其他被定义为公共静态的例程。我正在尝试实例化一些帮助我进行通信的辅助类,这些类需要在其他例程中可用(ReadCallback等)。因为我们没有实例化Program类,所以我在Program级别定义的所有东西(所有其他例程都可以看到它)必须被定义为static。所以我的问题是,是否有一种方法(没有将这些实例化的辅助对象传递到所有这些例程中),以使这些实例化的辅助对象可用于其他例程(如全局变量,但不是静态的)。感谢任何和所有帮助。

解决方案

尝试使任何全球化都是一个坏习惯。为什么?



静态方法虽然非常有用,但只有在它们仅作为参数传递的对象时才使用它们,加上(可选)返回对象,没有其他;它将帮助您避免静态变量,这些变量具有潜在的危险性,特别是对于多线程。



请参阅我的过去的答案,了解静态和实例成员的一些解释:

在c#中输入类型 [ ^ ],

C#windows基础这个关键词相关及其在应用程序中使用 [ ^ ],

是什么让静态方法可以访问? [ ^ ]。



如果你真的需要一个全局(每个应用程序域唯一的)对象,考虑使用单例模式

http://en.wikipedia.org/wiki/Singleton_pattern [ ^ ],

http://csharpindepth.com/文章/ General / Singleton.aspx [ ^ ]。



另请参阅: http ://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial [ ^ ]。



-SA


在您的代码中,您可以轻松地实例化Progam类并创建一个对象。你有没有尝试过?



 var program = new Program(); 





在使用TCP时,您可能会考虑查看System.Net命名空间,该命名空间将提供有关使用网络的过程的基本但良好的文档。您可以使用那里的课程进行交流。



http://msdn.microsoft.com/en-us/library/System.Net(v=vs.110).aspx



要在后台运行应用程序,您可以考虑使用BackgroundWorkers等在后台运行任务并捕获事件。



http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v = vs.110).aspx


No。

如果你是一个静态方法,你只能访问静态变量和方法,除非你创建一个非静态类的实例 - 在这种情况下你可以通过实例访问它的非静态方法和变量。



你可以伪造它 - 通过创建一个静态变量,它包含一个持有你的全局的类的实例变量,但坦率地说这也是一团糟。



为什么不做WinForms Main方法的工作:在主窗体上创建一个实例并让它完成工作?



 静态  int  Main( string  [] args)
{
MyListener listen = new MyListener();
while (listen.Run())
{}
}


I'm new to building console applications that will continue to run in the background (in C#). I used Visual Studio 2010 to produce the template, so it produced:

namespace MyNameSpace 
{ 
   class Program 
   { 
      static int Main(string[] args) 
      { 
      }
   } 
}



The program is a TCP listener, so I have a continual loop that listens for TCP messeges. Without getting to far off track, in Class Program I have other routines beside the Main routine, an AcceptCallBack, ReadCallback and others that are defined as public static. I'm trying to instantiate some helper classes that I need to help with the communication, that need to be available in those other routines (ReadCallback and others). Because we don't instatiate the Program class, anything I define at the Program level (so all the other routines can see it) has to be defined as static. So my question is, is there a way (short of passing those instantiated helper objects into all those routines) to make those instantiated helper objects available to the other routines (like a global variable, but one that isn't static). Thanks for any and all help.

解决方案

Trying to make anything global is really a bad habit. Why?

Static methods, though, are very useful, but only use them if they work only with the object passed as parameters, plus (optional) return object, nothing else; it will help you to avoid static variables, which are potentially dangerous, especially for multithreading.

Please see my past answer for some explanation of static and instance members:
Type casting in c#[^],
C# windows base this key word related and its uses in the application[^],
What makes static methods accessible?[^].

If you really need a "global" (unique per application domain) object, consider using the singleton pattern:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

See also: http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial[^].

—SA


In your code, you can easily instantiate the Progam class and create an object. Did you even give it a try?

var program = new Program();



Second while working with TCP you might consider looking into the System.Net namespace which would provide the basic but good documents about procedures for working with Networks. You can use classes given there to communicate.

http://msdn.microsoft.com/en-us/library/System.Net(v=vs.110).aspx

To run the app in background you can consider using the BackgroundWorkers etc to run the tasks in the background and capture events.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx


No.
If you are in a static method, you can only access static variables and methods, unless you create an instance of a non-static class - in which case you can access it's non-static methods and variables via the instance.

You can fake it - by creating a static variable which contains an instance of a class holding your "global" variables, but frankly that's a mess as well.

Why not do what the WinForms Main method does: creates an instance on the main form and lets it do the work?

static int Main(string[] args)
    {
    MyListener listen = new MyListener();
    while (listen.Run())
         {}
    }


这篇关于C#中的控制台应用程序使用静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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