This article mainly introduced the C # flexible use of the method of the class, with a good reference value, followed by a small series to see it
constructor function
Generalization: A constructor is a special method in a class that accomplishes the initialization of the object and completes the specified work when the object is created. And the constructor method name is the same as the class name, and there is no return value type.
No parameter constructor
By default, the class is assigned an parameterless constructor, and there is no method body. But we can also customize a parameterless constructor to automatically give the property a default value when creating the object.
Class Demo {public string Demoname {get, set;} public demo ()//Create parameterless constructor {this. Demoname = "no parameter constructor"; Method body write the property to initialize when creating the object}}
Tip: The shortcut to creating a parameterless constructor in Visual Studio is ' ctor ' + two tab key; the shortcut to creating the property is ' prop ' + two tab.
With parameter constructors
Sometimes we need to specify some values for the object's properties when we create the object, and these values are certain, so we need to take the parameter constructor.
Class Demo {public string Demoname {get, set;} public Demo (string demoname)//Create parameter constructor {this. Demoname = Demoname; }} class Test {Demo demo = new Demo ("with parameter constructor");//Specify values in parentheses when creating objects}
There are several things to keep in mind:
1. When creating an object, the arguments given in parentheses must be the same as the parameter list for the argument constructor.
2. With the parameter constructor in the class, creating the object must give the corresponding value within the parentheses. Because once the class has a constructor in C #, the constructor is no longer automatically assigned. (It is recommended that you create a parameter constructor and then create an parameterless constructor.) )
Additional:
There is another way to initialize properties quickly, that is, the object initializer.
Usage: Demo demo = new Demo () {Name = "object Initializer"};
Or: Demo demo = new demo{Name = "Object Initializer"};
Method overloading
Summary: The same thing, according to different parameters to perform different operations, that is, method overloading.
Features: Each method name is the same, the argument list is different, in the same class.
Note: Different parameter lists refer to different parameter types for each method or different order of parameters or number of parameters.
Only the return value is different and does not constitute a method overload.
Cases:
Class Demo {public string Demoname {get; set;}/********* constructor overload ********/public Demo () {this. Demoname = "no parameter constructor"; Public Demo (String demoname) {this. Demoname = Demoname; }/********** method overload **********/public void SayHello (string name) {Console.WriteLine ("Hello, I'm {0}, nice to meet you.") ", name);} public void SayHello (string name, int ages) {Console.WriteLine ("Hello, I am {0}, this year {1} years old, it is a pleasure to meet you.") ", name, age);}} Class Test {/********* constructor overload ********/Demo Demo = new Demo (); Demo Demo1 = new Demo ("with parametric constructor"); /********** method overload **********/public void Test () {demo. SayHello ("Xiao Ming"); Demo. SayHello ("Xiao Ming", 18); } }