C # New Features of 3.0

Source: Internet
Author: User

/** = ===============================* 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 );}
 
}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.