Is a Static Class a reference type or a value type?

Is a static class a reference type or a value type? it would be very helpful if someone gave a good explanation

+4
source share
5 answers

A class is always a reference type, whether it is static or non-static.

+14
source

I think you mean members of a static class .. and they are reference types if they are actually objects, otherwise they are just value types. a static class cannot be passed 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 cases EDIT: -
and yet, if you dig a little deep, a static class is basically a class with a private constructor and an unrelated state (variables) (unlike the example). so YES in theory this is a reference type

+1
source

The value and type of reference refers to type instances. A static class cannot be created and therefore this question does not apply to static classes.

a static class can contain only static members, and static members (for example, properties) are created once for the entire application, so if you change its value, it changes everywhere in your application.

+1
source

Yes, static classes are considered as reference types, since when you change the value of StaticClass.Property inside a method, this change will be filled in wherever you refer to this class. It has only one memory address and cannot be copied, so when another call to the method or property occurs, this new value will prevail over the old.

0
source

I just want to add that a static class is a class and a type. The static constructor is called once, so it is created by the "CLR" as a type when it is first "referenced" in the program, and therefore is the reference type of the class.

But I like to think about the implementation, or rather, the “use” of a static class as an empty type or “class name”, as it really is how its members are accessible and how they are executed. Therefore, think that the created Static Class is a hollow pointer to its methods and properties, and not as a pointer to a real instance of a class object that contains these things as a true non-static class.

0
source

All Articles