usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacethe difference between static and non-static { Public classPerson { Public voidM1 () {Console.WriteLine ("I am a non-static method"); } Public Static voidM2 () {Console.WriteLine ("I am a static method"); } }
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacethe difference between static and non-static {classProgram {Static voidMain (string[] args) {Person P=NewPerson (); P.M1 ();//instance MethodPerson. M2 ();//Static MethodsConsole.ReadLine (); } }}
The difference between static and non-static:
1) in non-static, there can be either an instance member or a static member.
2) When invoking an instance member, you need to use the object name. Instance Members
When you call a static member, you need to use the class name. Static member name
Summarize:
1) Static members must be called using the class name, and instance members are invoked using the object name
2) In an instance function, you can use either a static member or an instance member
3) in a static function, only static members are accessible, and access to instance members is not allowed
Use:
1) If you want your class to be used as a "tool class," consider writing the class as a static
2) Static classes share resources throughout the project
C # The difference between static and non-static members of classes in object-oriented