C # study notes 10,

Source: Internet
Author: User

C # study notes 10,

1.Anonymous type: the anonymous type is a new feature of C #3.0, which is a strong type (the Code definition type automatically declared when the compiler is generated as a pencil in the background ), after declaring and initializing attributes, the attributes are read-only. Only when the attribute names, sequences, and types are consistent can multiple declared anonymous variables share the same anonymous type. The ToString () method overwrites the returned text of the attribute name and value.

2.Implicit local variables: when using var to declare local variables, note the following two points:

(1) The type declaration on the Left can be clearly known from the type on the right of the value "=", for example, new FileInfo (""). It can be clearly known that the file is vigorous and var can be used. However, if the right side is a method call, the type should be explicitly declared. It is easy to know the return type, Instead of viewing it inside the method.

(2) var is generally used when the anonymous type is used, because the compiled type cannot be clearly known. However, explicit declarations should be used in standard query operators such as Linq. For example, IEnumerable <string> result is more readable than var result.

3.Do not modify the set in a foreach loop.

4.Set initializer: To compile successfully, the set initiator must meet one of the two points:

(1) Ideally, the set should implement the ICollecation <T> interface, which ensures that the Set supports an Add () method. The code generated by the compiler can call this Add () method () method,

(2) In the case of loose, the set should implement the IEnumerable <T> interface, which has one or more Add () methods, even if the set type does not implement the ICollecation <T> interface, the Add () method must accept parameters that are compatible with the values specified in the Set initializer.

 Note,The Set initiator cannot be directly used for the anonymous type, because the set initiator needs to call the constructor once, but you cannot name the constructor at all. There are two ways,

(1) define a method like the following. Call this method to create a set of anonymous types (static List <T> CreateList <T> (T t) {return new List <T> ();}).

(2) Use Arrays for processing, such as var items = new [] {...}.

5.Standard query operator: When where () and select () are used, data is processed vertically and horizontally for the set. Most of the actions that are delayed when using Linq, so pay attention to the need for immediate execution in use. the PLinq (parallel Linq) function is added in Net4.0. It uses AsParallel () of the Set. It is a member of the ParallelEnumerable class and is an extension method. Then you can use parallel query or data filtering,

For example, list. AsParallel (). Where (t => t. Name. StartWith ("")).

6.When using Linq for ascending sorting, OrderBy () is used for the first sorting method, while ThenBy () is used for other sorting methods. The same applies to descending sorting.

7.Join and GroupJoin: the former is the inner Join of two sets (equivalent to the inner Join of SQL); the latter is the group Join (Implementing One-to-multiple relationships ), that is, the elements of the right set corresponding to the Left Group.

8.Left outer: Implemented Using GroupJoin (). SelectMany () and DefaultIfEmpty (). You can view the code in the CorporateData. GetLeftInnerData () method.

9.We occasionally process a set composed of sets. If each item requires double nested traversal, we can use the SelectMany () method to process it here, it can be a collection to become a single new collection.

Public class CorporateData {public static Department [] Departments = new Department [] {new Department () {Id = "1", Name = "Legal Department"}, new Department () {Id = "2", Name = "Information Department"}, new Department () {Id = "3", Name = "Marketing Department"}, new Department () {Id = "4", Name = "Finance Department"}, new Department () {Id = "5", Name = "Administration Department "}}; public static Employee [] Employees = new Employee [] {new Employee () {No = "01", Name = "Liu Yi", CallName = "supervisor ", extends mentid = "1"}, new Employee () {No = "01", Name = "Xu Er", CallName = "supervisor", extends mentid = "1 "}, new Employee () {No = "01", Name = "Zhang San", CallName = "supervisor", Principal mentid = "3"}, new Employee () {No = "01", Name = "", CallName = "", dimensions mentid = "3"}, new Employee () {No = "01 ", name = "", CallName = "", dimensions mentid = "2"}, new Employee () {No = "01", Name = "Zhu ", callName = "", DepartmentId = "2"}, new Employee () {No = "01", Name = "Yan Qi", CallName = "", extends mentid = "4"}, new Employee () {No = "01", Name = "Mao ba", CallName = "supervisor", extends mentid = "4 "}, new Employee () {No = "01", Name = "", CallName = "", DepartmentId = "4"}, new Employee () {No = "01", Name = "10th", CallName = "", DepartmentId = "4" }}; // public static void GetLeftInnerData () is processed for the left Outer Join of the set () {var result = scripts. groupJoin (Employees, d => d. id, e => e. required mentid, (d, dEmployees) => new {d. id, d. name, Employees = dEmployees }). selectparameters (repeated mentemployees => specified mentemployees. employees. defaultIfEmpty (), (d, e) => new {d. name, employee = e}); foreach (var item in result) {Console. writeLine ("department: {0}, employee: {1}", item. name, item. employee) ;}} // group set public static void GetGroupBy () {IEnumerable <IGrouping <string, Employee> result = Employees. groupBy (e => e. extends mentid); foreach (IGrouping <string, Employee> item in result) {Console. writeLine (item. key); foreach (Employee in item) {Console. writeLine ("\ t {0}", employee) ;}}} public class Department {public string Id; public string Name;} public class Employee {public string No; public string Name; public string CallName; public string extends mentid; public override string ToString () {return string. format ("Name = {0}, CallName = {1}", Name, CallName );}}

--------------------------- The above content is organized according to the C # Third Edition of the essence.

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.