简体   繁体   中英

C# memory allocation: Difference between static class and a static instance

How does CLR allocates memory in these 2 cases:
Are they both allocated in static memory area?

static class A 
{
    //Some methods
}

and

class A 
{
    //Some methods    
}
class B
{
    static A inst = new A();
    //Some methods
}

For the below statement, will the compiler

static A inst = new A();

allocate A in heap and assign it to static reference inst reference? Or it will create a static instance in High Frequency heap ?

Static fields are as any static fields, regardless if the class is static or static.

Static class is just a language tric to ensure that all members are static, nothing more. In .NET static classes do not even exists: A static class will be converted into a "normal" class with a private instance constructor. That way an instance of that class cannot be constructed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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