简体   繁体   English

C#在类外部创建静态对象与在类内部创建静态对象有什么区别?

[英]C# What is the difference between creating a static object outside a class VS creating it inside a class?

I want to understand the difference between 3 sets of snippets below: 我想了解以下3组摘要之间的区别:

private static FirstObject o = new FirstObject();
public class ClassA
{
}

//-----------------------------------------------------

public class ClassA
{
    private static FirstObject o = new FirstObject();
}

//-----------------------------------------------------

public class ClassA
{
    private static FirstObject o;

    public ClassA
    { 
        o = new FirstObject();
    }
}

Please help me understand in terms of scope, memory, performance and usage of these. 请帮助我了解这些工具的范围,内存,性能和用法。

Thank you. 谢谢。

  1. Invalid, as you can't have a variable outside of object 无效,因为您不能在对象外部有变量

  2. The proper way - the class has a static member, which is initialized when the class is accessed for the first time 正确的方法-该类具有静态成员,该静态成员在首次访问该类时初始化

  3. Very bad, because every time when new object is created the static object will be recreated. 非常糟糕,因为每次创建新对象时,都会重新创建静态对象。

The first option will not compile. 第一个选项将无法编译。 A static variable in C# must be scoped to a class or struct. C#中的静态变量必须作用域为类或结构。

The second option is the preferred mechanism. 第二种选择是首选机制。

The third option is wrong because this creates a new FirstObject each time an instance of ClassA is created, which is almost certainly not what you want. 第三个选项是错误的,因为每次创建ClassA实例时都会创建一个新的FirstObject ,这几乎肯定不是您想要的。

A fourth option would be to leverage a static constructor, eg, 第四个选择是利用静态构造函数,例如,

public class ClassA
{
    private static FirstObject o;
    static ClassA
    {
        o = new FirstObject();
    }
}

This option is useful if there is some special construction constraints for FirstObject . 如果FirstObject有一些特殊的构造约束,则此选项很有用。 In this example, though, choose option 2 over option 4. Just know that option 4 exists. 但是,在此示例中,选择选项2而不是选项4。只知道选项4存在。

Three cases below... 以下三种情况...

  1. Assuming a typo here missing some outer construct... "o" is declared so that it will be globally accessible, as a single object, to the entire application. 假设此处的拼写错误缺少某些外部构造...声明为“ o”,以便可以作为单个对象将其全局访问整个应用程序。 It will have one common set of all properties and data. 它将具有一组通用的所有属性和数据。 It can be access directly by "Namespace.o" 可以通过“ Namespace.o”直接访问
  2. "o" is declared so that it will be globally accessible, as a single object, to the entire application, However it is only accessible through another defined instance of "ClassA". 声明了“ o”,以便可以作为单个对象全局访问整个应用程序,但是只能通过“ ClassA”的另一个定义实例进行访问。 Each separate instance of ClassA will have the same, single "o" object with the same properties and data. ClassA的每个单独实例将具有相同的,具有相同属性和数据的单个“ o”对象。
  3. This doesn't look right to me, I'm assuming "ol" is supposed to "o;". 这在我看来不对,我假设“ ol”应该为“ o;”。 Even with this the code looks like its missing something. 即使这样,代码看起来也缺少一些东西。 if the Line "o = new FirstObject" is correct it is not accessible in this fashion. 如果“ o = new FirstObject”行正确,则无法以这种方式访问​​。

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

相关问题 在循环内部和外部创建对象之间的内存区别是什么 - What's the difference in memory between creating an object inside and outside of a loop OOPS概念:在C#中传递对象引用和创建类对象有什么区别? - OOPS Concepts: What is the difference in passing object reference to interface and creating class object in C#? 创建类和inteface的对象有什么区别(参见下面的例子)? - what is the difference between creating object of class and inteface(see following example)? 如何在其他类中使用非静态方法而不在C#中创建对象 - how to use non static method inside other class without creating object in C# 在 C# 中创建 object 的实例时,带和不带 () 符号有什么区别? - What is the difference between with and without () symbol in creating an instance of an object in C#? 静态类和命名空间有什么区别? (在 C# 中) - What is the difference between a static class and a namespace? (in C#) 在C#中创建一个类对象 - Creating a class object in C# 将类声明为静态和在app.xaml文件中创建实例有什么区别? - What is the difference between declaring a class as static and creating an instance in app.xaml file? 在C#中创建一个类 - Creating a class in c# 在C#中使用同一类的对象创建属性 - Creating a property with the object of the same class in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM