简体   繁体   English

如何在C#中定义全局结构?

[英]how to define a Global Structure in C#?

I want to define a global structure in C# and use it in three separate subroutines. 我想在C#中定义一个全局结构,并在三个独立的子程序中使用它。 I have already created the structure but I don't know how to make it a global variable so I can use it in multiple sections of my code. 我已经创建了结构,但我不知道如何使它成为一个全局变量,所以我可以在我的代码的多个部分中使用它。 Any help is appreciated. 任何帮助表示赞赏。

    public struct Simple
    {
        public int Position;
        public bool Exists;
        public double LastValue;
    };


    static void Main(string[] args)
    {
        Simple s;
        s.Position = 1;
        s.Exists = false;
        s.LastValue = 5.5;
    }

So I want to use a Simple structure in two other routines in my code and possible pass it to different form (multiple usage of one variable). 所以我想在我的代码中的其他两个例程中使用Simple结构,并可能将它传递给不同的形式(一个变量的多次使用)。

The closest thing to "global" in C# is "static". C#中最接近“全局”的是“静态”。 Simply define the class and all members as static and it'll be accessible from anywhere the containing namespace is referenced. 只需将类和所有成员定义为静态,就可以从引用包含命名空间的任何位置访问它。 EDIT as Servy correctly points out, the class itself does not have to be static; 正如Servy正确指出的那样, 编辑本身并不一定是静态的; however doing so forces all members to be static at compile-time. 但这样做会强制所有成员在编译时保持静态。 Also, static members can have any visibility, so you can have a private static field used by a public static property or method, and you can have an internal static class that won't be visible outside its home assembly. 此外,静态成员可以具有任何可见性,因此您可以拥有公共静态属性或方法使用的私有静态字段,并且您可以拥有在其主程序集外部不可见的内部静态类。 Just being static doesn't automatically make it wide open. 只是静态不会自动使它大开。

However, a better pattern might be the Singleton; 但是,一个更好的模式可能是Singleton; you define a class that has one static instance of itself, that can then be passed around. 你定义了一个具有一个自身静态实例的类,然后可以传递它。 The benefit is that you can still deal with the object as an instance class if you want to, but the same instance is available everywhere using a static getter. 好处是,如果您愿意,您仍然可以将对象作为实例类处理,但是使用静态getter可以在任何地方使用相同的实例。 Here's some reading material: http://csharpindepth.com/Articles/General/Singleton.aspx 这里有一些阅读材料: http//csharpindepth.com/Articles/General/Singleton.aspx

In your case it appears that you have a object as a local variable in your main method that you need to use in another method. 在您的情况下,您似乎在主方法中有一个对象作为本地变量,您需要在另一个方法中使用该对象。 The appropriate solution in this context is to add a parameter to that other method. 此上下文中的适当解决方案是将参数添加到该其他方法。 Take a look at this example: 看看这个例子:

public class MyObject
{
    public int Value;
}

public static void Main(string[] args)
{
    MyObject obj = new MyObject();

    obj.Value = 42;

    PrintObject(obj);

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey(true);
}

public static void PrintObject(MyObject obj)
{
    Console.WriteLine(obj.Value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM