1. Static and non-static members can appear in non-static classes
usingSystem;namespaceshuzu{classDom_class { Public stringName ="Zhang San"; Private intAge ; Public intAge//accessing private members through properties { Get{returnAge ;} Set{age =value;} } } classAClass {Static voidMain (string[] args) {Dom_class s=NewDom_class (); S.name="ABC"; S.age= the; Console.WriteLine (S.name); Console.WriteLine (S.age); Console.readkey (); } }}
View Code
2. Static members can only appear in static classes, and object creation is not allowed
usingSystem;namespaceshuzu{Static classAClass {Static inti = the; Static voidMain (string[] args) {i= -;//accessing static MembersConsole.WriteLine (ACLASS.I);//You can also access thisConsole.WriteLine (i); Console.readkey (); } }}
View Code
3. Differences on non-static and static member invocations (class name point object name) non-static (object name point member name)
usingSystem;namespaceshuzu{classDom_class { Public Static intShu = -;//access to static members } classAClass {Static voidMain (string[] args) { intg= Dom_class.shu = the;//The class name. Object Name AccessConsole.WriteLine (g); Console.readkey (); } }}
View Code
usingSystem;namespaceshuzu{classDom_class { Public stringName ="Zhang San";//access to non-static members Private intAge ; Public intAge//accessing private members through properties { Get{returnAge ;} Set{age =value;} } } classAClass {Static voidMain (string[] args) {Dom_class s=NewDom_class (); S.name="ABC";//by establishing object accessS.age = the; Console.WriteLine (S.name); Console.WriteLine (S.age); Console.readkey (); } }}
View Code
4. When do I use static classes? Use static classes when used as a tool class
C # static and non-static classes (instance classes)