Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

C# - Each Constructed Type will maintain its own copy of static variable

Aug 10 2011 5:24AM by Ramireddy   

We all know that a static variable will be shared by entire class instead of objects. What about Generic Types?? Suppose, from a generic type, if 2 constructed types created, will they maintain different copy or same copy?? They will maintain their own copy.

class A<T>
{
    static int M;
    public static void addnum()
    {
        M++;
        Console.WriteLine(M);
    }
}
class C
{
    public static void Main()
    {
        A<int> a = new A<int>();
        A<int>.addnum();
        A<int> a2 = new A<int>();
        A<int>.addnum();
        A<string> a3 = new A<string>();
        A<string>.addnum();
        Console.ReadKey();
    }
}

Don't be confused... Constructed Types are classes only, where as Generic Types are like templates. Static variable will be maintained in class level. So, constructed types will maintain their own copy.

Read More..   [32134 clicks]

Published under: Microsoft .NET Tips ·  ·  ·  · 


Ramireddy
2 · 40% · 12972
4
 
0
Knew
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

Your Comment


Sign Up or Login to post a comment.

"C# - Each Constructed Type will maintain its own copy of static variable " rated 5 out of 5 by 4 readers
C# - Each Constructed Type will maintain its own copy of static variable , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]