.NET FAQs Unleashed!
 
    



    


THE GRAND DADDY OF ALL SEO SOFTWARE! GET A TOP 5 GOOGLE RANKING IN UNDER 30 DAYS!

What is the difference between Shared and Static?

They both mean the same. Shared is used in VB.NET. Static is used in C#.
When the static keyword is used to declare a class, the member in context must be directly invoked from the class, rather than from the instance. Consider the following example

//Consider writing the following line of code...
Console obj = new Console();
obj.Writeline("Vishal likes static members"); //This line does'nt print

//This does'nt work, because WriteLine is a static method defined in the class Console
//The Console class is a static class

To use static members, give a reference to the exact class, as an instance in this case won't work.

To make this work, write...
Console.Writeline("Vishal likes static members");

To work with members of static classes, no need to create their instances.
Static Member - A class member declared with the keyword static is a static member. A static member is owned by the class, not by its instances (objects of the class).
Note that static members are actually class members, while non-static members are instance members (means they are owned by the instances). Both in C# & VB.NET, we may create static/shared events, properties, fields and functions.

Note Indexers in C# cannot be declared static.

Note Static member functions cannot access non-static members directly.

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 




ShoutMix chat widget





Know Your Breath, Know Your Life!!!