/** = ===============================* 1. Automatic attributes * 2. Object and set initiators * 3. Extension methods * 4. Lambda expressions * 13/6/2010 Berlin **/using System; using System. collections. generic; using System. linq; using System. text;
Namespace LinqTest {
// Demonstrate automatic attributes public class Point {public int X {get; set;} // automatic attributes
Public int Y {get; private set;} // automated attributes with Modifiers
Public int T {get; set ;}}
// Demonstrate the object and set initialize; class Test_45 {Point p = new Point {X = 23, T = 2}; // You Can instantiate an object in this way.
// Initialize a collection List <Point> PoList = new List <Point> {new Point {X = 2, T = 3}, new Point {X = 4, T = 2}, new Point {X = 23, T = 90 }};}
// Demonstrate the extension method. The so-called extension method refers to the compiled assembly in C #3.0, add related methods in programs that do not need to re-compile the source code or do not disclose the source code. // comments. If you want to call a third-party API without the source code, you can extend the functions, use it. // implementation: Use a static class. The first parameter of the static method is used to input the class name to be extended, for example, this class object.
Static class ExtensionsFunction {// extended method GetZeroPoint public static Point GetZeroPoint (this Point p) {return new Point {X = 0, T = 0 };}
// Added the public static void ShowSelfInfo (this Test_45 obj) {Console. writeLine (obj. getType (). assembly. fullName );}}
// Use the extension method public class TestExtension {public void TestExtenSionFuntion () {Point p = new Point {X = 8, T = 9}; Test_45 t = new Test_45 ();
P. GetZeroPoint (); // call an extension method of Point. Note that this method does not exist in the class Point.
T. ShowSelfInfo ();}}
// Demonstrate Lambda expressions // Lambda expressions are a simplified anonymous method // There are several forms of representation // (int x) =>{ return x + 1 ;} // (int x) => x + 1; // x => x + 1; // (x, y) => x + y; public class LambdaTest {public void TestLambda () {List <Point> pList = new List <Point> {new Point {X = 2, T = 3 }, new Point {X = 4, T = 1 }};
List <Point> ppList = new List <Point> (); ppList = pList. FindAll (p => p. X> 2 & p. T <3 );}
}}