- Method overloading: in the same scope, there are multiple methods with the same method name but different parameter lists. Different parameter lists include: number, type and order of parameters
static void Main(string[] args)//Main method { Print(); Print(12); Print(12,12); Print("faaf"); Print("asdf", 15); Print(17,"sdgg"); Random rand = new Random(); int a = rand.Next(); Console.WriteLine(a); } public static void Print() { Console.WriteLine("Method overload 1"); } public static void Print(int a) { Console.WriteLine("Method overload 2"+a); } public static void Print(int a,int b) { Console.WriteLine("Method overload 3"+a+b); } public static void Print(string a) { Console.WriteLine("Method overload 4"+a); } public static void Print(string a,int b) { Console.WriteLine("Method overload 5"+a+b); } public static void Print(int a, string b) { Console.WriteLine("Method overload 6" + a + b); } }
- Constructor: create a class object, and the compiler will automatically call the constructor to initialize the object
Declaration and call of constructor: static void Main(string[] args) { person P = new person("Constructor",200); P.print(); } class person { private string name; private int age; public person(string n,int a) { name = n; age = a; } public person(string n = "Constructor takes parameters and has default values", int a = 300) { name = n;//There are two output sentences: constructor, 200 age = a;//No output:, 0 (default) } public void print() { Console.WriteLine("{0}{1}",name,age); } }
- Default constructor: if there is no constructor in the class, the compiler will automatically provide an empty constructor.
- public person() {...} without parameters
- With parameters, but all parameters have default values
- There is no default value with parameter
- Constructor features:
- A class can contain multiple constructors -- overloads of constructors
- Default constructor
- Constructor name is consistent with class name
- Constructor has no return value
- Constructors are generally public
- Static constructor: used to initialize static data members
- Static constructors cannot have access modifiers and parameters
- The static constructor is called only once
- Static constructors cannot access instance members// Static members need to be modified by static keyword, while instance members do not need to be modified by static keyword.
- Static constructors cannot be called directly
- Static constructors are called before creating the first instance or before referencing any static members
static void Main(string[] args) { student s = new student("Ordinary students", 100); s.print(); student s1 = new student("Ordinary students", 100); s1.print(); } //Static constructor called //Ordinary students, 100, static construction //Ordinary students, 100, static construction class student { private static string name; private int age; private static string teachername; //public static string tN { } //public student(string n, int a,string ta) { name = n; age = a;teachername = ta; } public student(string n, int a) { name = n;age = a; } static student()//Static construction method { teachername = "Static construction"; Console.WriteLine("Static constructor called");//Will only be called once } public void print() { Console.WriteLine("{0},{1},{2}", name, age, teachername); } }
- Destructor: used to release the managed and unmanaged resources used by the object before it is destroyed. For most objects in C #, GC can be used to implicitly perform memory management tasks. However, if unmanaged resource objects are encapsulated, after the application uses these unmanaged resources, the garbage collector will run the object destructor to release these resources
characteristic:
- Can be defined in a structure, only in a class
- A class can only have one destructor
- Destructors cannot be inherited or overloaded
- Call destructors that cannot be displayed are automatically called by the garbage collector
- There are no access modifiers and no parameters
static void Main(string[] args) { xigou x = new xigou(); } class xigou { public xigou() { Console.WriteLine("Constructor called"); }//Constructor ~xigou(){ Console.WriteLine("Destructor called"); }//Destructor }
readonl:
- It can be initialized at declaration time or in the constructor
- Only class members can be decorated
const:
- Must be initialized at declaration time
- You can modify class members or method members
class personn { private string name; private readonly int age = 6; private const int id = 1; public personn(string n, int a) { name = n;age = a; } public void print() { Console.WriteLine("{0},{1},{2}",name,age,id); } }