When we are about to enter the ASP. We need to learn some C # specific syntax first. And for the development of traditional webform, we introduce these C # specific grammars that are not familiar to everyone. This section describes the C # language features required by an MVC good programmer.
One: Basic features of C #
1.1 Using auto-implemented properties
The C # attribute feature lets you expose a class of data fragments that are loosely coupled to how data is set and received. This means that it is not directly associated with the fields in our class, but through an external interface. Let's take a look at the following, which is called the "Product" class, and we only have one field .
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceHenan Cloud and data { Public classProduct {Private stringname; Public stringName {Get{returnname;} Set{name =value;} } }}
The name is a property of "name". A statement in a Get code block (which becomes a getter) executes when the value of the property is read, and the statement in the Set code block (the setter) is executed when the property is assigned a value (the special variable value represents the values to be assigned). A property is used by other classes as if it were a field, in summary. In the above class, name is a field. And name is a property.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingHenan Cloud and data;namespacecw{classProgram {Static voidMain (string[] args) {Product MyProduct=NewProduct (); Myproduct.name="Henan Cloud and data Rocky Ren"; stringProcductname =Myproduct.name; Console.WriteLine ("Product name is {0}", Procductname); Console.readkey (); } }}Value by property assignment
As you can see, the reading and setting of the property value is much better than using the field, just like reading a field of a rule, because you can modify the statements in the get and set blocks without having to modify the entire class that depends on the property. However, when there are many attributes in a class, things become dull, and all getters and setters do the same thing-fill in the fields. So we have to do some more lengthy things.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceHenan Cloud and data { Public classProduct {Private intID; Public intId {Get{returnID;} Set{id =value;} } Private stringname; Public stringName {Get{returnname;} Set{name =value;} } Private stringdeacription; Public stringdeacription {Get{returndeacription;} Set{deacription =value;} } Private decimalPrice ; Public decimalPrice {Get{returnPrice ;} Set{Price =value;} } }}
One thing to note here is that we can quickly encapsulate these fields in the field (Ctrl+r+e).
We want the properties to be flexible, but at the moment we don't need a custom getter and setter, the solution is an auto-implemented property, and it becomes an " automatic attribute" he doesn't need getter and setter .
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceHenan Cloud and data { Public classProduct { Public intId {Set;Get; } Public stringName {Set;Get; } Public stringDescription {Set;Get; } Public decimalPrice {Set;Get; } Public stringCategory {Set;Get; } }}
There are two key points to note when using automatic attributes. The first is to not define the body of getter and setter, and the second is to not define the font returned by this property. Both are done automatically by the C # compiler when this class is built. Using automatic attributes is no different from using rules, creating code that facilitates code reading, and still retains the flexibility of attributes, and returns the format of a rule property if you need to change the way a property is implemented.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceHenan Cloud and data { Public classProduct {Private intID; Public intId {Set;Get; } Private stringname; Public stringName {Set;Get; } Private stringdescription; Public stringDescription {Set;Get; } Private stringPrice ; Public decimalPrice {Set;Get; } Public stringCategory {Set;Get; } }}
Working with objects and collection initializers