Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the essence of Linq query

Shulou Source: shulou.com Published: 2022-06-02 09:00:10 09月21日 Update

This article mainly introduces "what is the essence of Linq query". In daily operation, I believe many people have doubts about the nature of Linq query. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the essence of Linq query?" Next, please follow the editor to study!

1. Automatic attribute

Class Class {/ / id 2.0 attribute / / private int _ id; / / public int ID / / {/ / get {/ / return _ id; / /} / / set / / {/ / _ id = value; / /} /} / / get set 3.0 attribute can add an access modifier public int ID {get; private set;} public string Name {get; set } public Class (int id) {/ / attributes with private can only be accessed inside the class this.ID = id;}}

The essence of Linq query: it is no different from the original properties, but simplifies the syntax.

two。 Initializer

Private static void Initializer () {/ / Class 2.0 object initialization / / Class c = new Class (1); / / c.Name = "* Class 1"; / / Cure 3.0 object initializer Class c = new Class (1) {Name = "* Class 1"}; / / Cure 2.0 collection initialization / / ClassCollection list = class 1 (); / / list.Add (c) / / Collection initializer ClassCollection list = new ClassCollection {new Class (1) {Name= "* Class 1"}, new Class (2) {Name= "* Class 2"}}; foreach (Class item in list) {Console.WriteLine (item.ID + "" + item.Name);}}

The essence of Linq query: it is no different from the original constructor initialization or construction through attribute initialization, which simplifies the syntax.

3. Local variables with implicit types

Private static void Var () {var icompiler 1; / / the compiled result is actually int iTunes 1; var is not a dynamic variable, its type is actually c # compiler int / / var I = DateTime.Now; / / compiled by context, but unlike JavaScript, var d = DateTime.Now; / / = supports various types of var a = new int [] {1,2,3}. / / var also supports the type of array foreach (var item in a) / / item inferred by the C # compiler to know that it is int {Console.WriteLine (I);} / var x; / / error, there is no initializer / / var y = {1,2,3}; / / error, collection initializer / / var z = null is not allowed / / error. Null type is not allowed.

Linq query essence: var is not a dynamic type, C # is still a static language, the introduction of var is convenient for us to write code, regardless of the type of assignment expression after "=", it is up to the compiler to infer and generate the corresponding type.

At this point, the study on "what is the essence of Linq query" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Types essentials queries properties compilers learning inference compilers errors dynamics variables actual objects more syntax attributes help support utility and then Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information MySQL Redmi Apple