LINQ series: LINQ-related features in C #

Source: Internet
Author: User

1. Anonymous type

By defining an anonymous type with the keyword VAR, the compiler emits a strong type based on the value of the expression on the right side of the operator.

Some basic rules to follow when using anonymous types:

◊ An anonymous type must have an initialization value, and the value cannot be null (NULL), because the type is inferred from the initializer;

◊ Anonymous types can be used for simple types, or for complex types. When defining a simple type, it is of little value. Compound anonymous types require a member declaration;

◊ Anonymous types cannot be used for fields of classes;

◊ An anonymous type can be used as an initializer in a for loop;

◊ You can use the new keyword; the initializer for the array must use the new keyword;

◊ Anonymous types can be used for arrays;

◊ all anonymous types are derived from the object type;

var " LINQ to Object ";

1.1 Compound anonymous Types

var product = new {Title = "LINQ to Object", UnitPrice = 10m};

1.2 Using anonymous types in For/foreach statements

var New int 1 1 2 3 5 8  -  +  };  for (var0; i < Fibonacci. Length; i++) {    Console.WriteLine (fibonacci[i]);}
var New int 1 1 2 3 5 8  -  +  }; foreach (varin fromwhere5Select  f) {    Console.WriteLine (number);}

1.3 Anonymous types and using

using var New SqlConnection (connectionString)) {    connection. Open ();     //  ......}

2. Array initializer

Initializes the array with the keyword new.

var New int 1 1 2 3 5 8  -  ;

3. Anonymous generic methods

func<Long,Long> factorial =Delegate(LongN) {    if(n = =1)    {        return 1; }    Longresult =1;  for(inti =2; I <= N; i++) {result*=i; }    returnresult;}; Console.WriteLine (Factorial (6));
//using System.Diagnostics;func<Long,Long> factorial =Delegate(LongN) {    returnn >1? n * (Long)(NewStackTrace (). GetFrame (0)        . GetMethod (). Invoke (NULL,New Object[] {n-1})): N;}; Console.WriteLine (Factorial (6));

4. Auto-Implement Properties

 Public string Get set; }

To set read-only properties:

 Public string Get Private set; }

5. Object and Collection initializers

Object initializer: Creates a new object in a declarative manner without invoking the constructor of the class, and sets the value of each property of the object.

New product{    1,    "LINQ to Object",    =  10m};

The collection initializer is used to initialize a collection.

New List<product> {     new1"LINQ to Object", UnitPrice = 10m},    new2"LINQ to ADO", UnitPrice =  20m}};

LINQ series: LINQ-related features in C #

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.