简体   繁体   English

Static Class是引用类型还是值类型?

[英]Is Static Class a reference type or a value type?

Whether static class is a reference type or a value type?静态类是引用类型还是值类型? it would be really helpful if any one gives a good explanation如果有人给出一个很好的解释,那将非常有帮助

Class is always a reference type irrespective of whether it is static or non-static.类始终是引用类型,无论它是静态的还是非静态的。

Value and Reference type is about instances of Types.值和引用类型是关于类型的实例。 A static class cannot be instantiated and hence this question is not relevant to static classes.无法实例化静态类,因此这个问题与静态类无关。

a static class can only contain static members and static members (like properties) are created once for the entire application, so if you change its value, it changes everywhere in your application.静态类只能包含静态成员,静态成员(如属性)为整个应用程序创建一次,因此如果您更改它的值,它会在您的应用程序中随处更改。

i think what you are referring to are members of static class.. and they are reference types if they are actually objects otherwise they are simply value types.我认为你指的是静态类的成员.. 如果它们实际上是对象,它们就是reference类型,否则它们只是值类型。 a static class inself cannot be passed around as for as i know.据我所知,静态类本身不能传递。 try running this code尝试运行这段代码

class Program
    {
        static void Main(string[] args)
        {
            StaticClass.x = 89;
            Console.WriteLine(StaticClass.x);
            changeValue(StaticClass.x);
            Console.WriteLine(StaticClass.x);
            Console.ReadKey();
        }
        static void changeValue(int x)
        {
            x = x + 1;
        }
    }
{
    public static class StaticClass
    {
        public static int x { get; set; }
    }
}

EDIT:-编辑:-
the output is 89 in both the cases两种情况下的输出都是 89
EDIT:-编辑:-
and still if you dig a little deep a static class is basically a class with a private constructor and no state(Variables) associated with it(unlike the example i have provided).而且,如果您深入研究,静态类基本上是一个具有私有构造函数并且没有与之关联的状态(变量)的类(与我提供的示例不同)。 so YES in theory it is a reference type所以理论上是的,它是一个reference类型

I just want to add that a Static Class is both a Class and a Type.我只想补充一点,静态类既是类又是类型。 The static constructor is called one time so it is "created" by the CLR as a Type when first "referenced" in a program, and so is a true class reference type.静态构造函数被调用一次,因此在程序中第一次“引用”时,它由 CLR 作为类型“创建”,因此是真正的类引用类型。

But I like to think of the implementation or rather "use" of a Static Class as an empty Type or "class name" only, as that's really how its members are accessed and how it performs.但我喜欢将静态类的实现或更确切地说“使用”视为空类型或“类名”,因为这实际上是其成员的访问方式和执行方式。 Therefore think of the created Static Class as a hollow pointer to its methods and properties only, not as a pointer to a real instantiated class object that contains those things as a true non-static class would.因此,将创建的静态类视为仅指向其方法和属性的空心指针,而不是像真正的非静态类那样指向包含这些内容的真实实例化类对象的指针。

Yes, static classes are considered as reference types as when you change a StaticClass.Property value within a method, this change will get populated everywhere you reference this class.是的,静态类被视为引用类型,因为当您在方法中更改 StaticClass.Property 值时,此更改将在您引用此类的任何地方填充。 It has only one memory address and can't be copied, so that when another method or property call will occur, this new value will prevail over the old one.它只有一个内存地址,不能复制,所以当另一个方法或属性调用发生时,这个新值将优先于旧值。

Non of them because you cannot copy it as value type and you cannot instantiate it as reference type.不是它们,因为您不能将其复制为值类型,也不能将其实例化为引用类型。 This question does not make sense but it does reside on stack like structs, int, double, but it does not allow you to be copied!这道题没有意义但是它确实像structs、int、double一样驻留在栈上,但是它不允许你被复制!

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

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