C # simple example of chained calls

Chained calls, repeated use of an initialization operation, a small amount of code to achieve the purpose of the expression of complex operations.

Direct look at an example

 1     public class Student
 2     {
 3         private string name;
 4         private int age;
 5 
 6         public Student() { }
 7 
 8         public Student setName(string name) 9  { 10 this.name = name; 11  System.Console.WriteLine(name); 12 return this; 13  } 14 15 public Student setAge(int age) 16  { 17 this.age = age; 18  System.Console.WriteLine(age); 19 return this; 20  } 21 }
1 Student person = new Student();
2 person.setAge (12) .setName ( "Jack"); // chain by calling

If SetAge () and SetName () return type of the class itself is not the case, such as the return string, int can not be used in this way the.

Guess you like

Origin www.cnblogs.com/YourDirection/p/12348440.html