Static, non-static
First look at a piece of code to differentiate between static and non-static:
using system;using system.collections.generic;using System.Linq;using System.text;using system.threading.tasks;namespace Static and non-static differences {/** * non-static classes * You can define static fields, static properties, static methods. * You can also define non-static (instance member) fields, non-static (instance member) properties, non-static (instance member) static methods. **///non-static class public class Person1 {//instance member (non-static) private int _id; Instance property (non-static) public int ID {get {return _id;} set {_id = value;} }//instance method (non-static) public void Showinfo () {}/*---------------------------------------------------- ----------------*///static field private static string _name; static property public static string Name {get {return _name; } set {_name = value; }}//static method public static void Sho () {}}}
You can see that the difference between static and non-static is the keyword: static
Static and non-static differences:
1), in a non-static class, there can be either an instance member or a static member.
2), when invoking instance members, 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;
3), non-static classes: You can define either static fields, static properties, static methods, or non-static (instance member) fields, non-static (instance member) properties, non-static (instance member) methods.
Now consider a small case of static and non-static:
static void Main (string[] args) { //Call instance member person p = new Person (); Non-static method p.m1 (); static method person.m2 (); Console.WriteLine (); Console.readkey (); } public class person { private static string _name; public static string Name { get {return person._name;} set {person._name = value;} } Private char _gender; Public Char Gender { get {return _gender;} set {_gender = value;} } public void M1 () { Console.WriteLine ("I am a non-static method"); } public static void M2 () { Console.WriteLine ("I am a static method");} }
Operation Result:
Summarize:
Static members must be called using the class name, and instance members are invoked using the object name.
In a static function, only static members can be accessed and instance members are not allowed.
Instance functions, you can use either static members or instance members.
Only static members are allowed in static classes, and instance members are not allowed.
Use:
1), if you want your class to be used as a "tool class", consider writing the class as static.
2), static classes in the entire project resource sharing. A static class frees resources only after the program has all finished.
This article is here, and finally make a small ad: QQ Group: . NET step by Step screen number:590170361 (Dabigatran Note: Blog Park to see)
. NET Foundation Step-by-step screen [object-oriented static, non-static]