Integration of common features and features of frameworks

Source: Internet
Author: User
Tags tojson

Integration of common features and features of frameworks

·Code segment. This feature has been known for a long time. The Framework already provides many code segments, and we can also customize code segments. However, it has never been used before. It is quite interesting to practice it today, this idea of Automatic Generation of code is actually quite useful. In addition, in the code segment provided by the Framework, the number of C # and VB is significantly different. I don't know why C # is less? I checked it online and said that MSDN provides extensions for the C # code segment. You can download it and try it out.

·Reconstruction. Previously, IDE was used in Eclipse for refactoring, which is very convenient. Now VS2005 has added this function, and I feel like it is similar to Eclipse. A better place is to maintain synchronization for different projects in the same solution during the reconstruction through VS2005, this is very useful.

·Debugging. VS2005 supports code modification in Debug mode, which is frequently used.

·Null type. You can specify true or false for a boolean type. See the following code.

Public void test () {int? I = null; int? Y = 4; int? X = I + y; Console. Write (x); // x is null}

·Null merge Operator. The null merge operator provides a quick expression to indicate possible null values when processing null and reference types.

1). If the first operand is not null, the entire expression is equal to the value of the first operand.

2) If the first operand is null, the entire expression is equal to the value of the second operand.

int? a=nullint b;b=a??10;//b=10;a=3;b=a??10//b=2;

If the second operand cannot be implicitly converted to the type of the first operand, a compilation error is generated.

Anonymous type

public object test()        {            return new { a="1",b="2"};        } dynamic dy = new ExpandoObject();            dy = test();            Response.Write(dy.b);

Ha, dynamic type 2.0 is unavailable. Here we borrow it first.

Partial class)

There is nothing to say about this. It is to split a class into two classes, but the class name must be the same. Controls that are common in. net are written to the partial classification during initialization.

Public partial class test {// <summary> // Head1 control. /// </Summary> /// <remarks> /// automatically generated field. /// To modify the field declaration, move the field declaration from the designer file to the code hidden file. /// </Remarks> protected global: System. Web. UI. HtmlControls. HtmlHead Head1; // This line}

Paste this line of code to the. cs file, and the program will run the same way.

FrameWork3.5 features

3.5 of the features are used more intentionally or unintentionally in daily work. The following is a list

This is a link I found when looking for version features.

This section introduces the new features of version 3.5.

. LINQ

There are too many people to discuss. Everywhere

· Lambda

Since C #3.0, you can use a new syntax to delegate the implementation code to Lambda expressions.

Lambda expressions can be used whenever there is a delegate parameter type.

The required parameters are listed on the left of the Lambda operator "=>. The right side of the Lambda operator defines the implementation code of the method that grants Lambda variables.

Public delegate string thisAction (string par); static void Main (string [] args) {thisAction lambda = a ==>{ return a ++ = "this is my test ";}; // can this be understood as using an anonymous function? // Generally, it should be thisAction lambda = functionName. String aa = lambda ("Hi,"); Console. Write (aa );}

Below:

  public delegate string thisAction(string par);        static void Main(string[] args)        {                 //  thisAction lambda = a => {return a += "this is my test";};       thisAction lambda=functionName;            string aa = lambda("Hi, ");            Console.Write(aa);} static string functionName(string par)        {            return par += "this is my test";        }

This should be the case. I didn't test it by hand.

Only one is to put the logic into Lambda, and the other is to encapsulate the logic with methods, and then directly assign the method name to the delegate. Oh, by the way, the comment should not be an anonymous function. It is just a method supported by lambda.

Therefore, we can also see that many people use the first method to write the logic into lambda, instead of encapsulating the logic into a method, and then call the method name. The clear idea is to look at the code written by others.

(In fact, I have always understood that lambda is used in collections. But I always like obfuscation .)

· Var implicit type variable

Type inference: the compiler will "deduce" the type of the variable based on the initial value of the variable.

Rules to be followed:

1) The variable must be initialized. Otherwise, the compiler does not deduce the variable type.

2) The initiator cannot be empty.

3) The initiator must be placed before the expression.

4) You cannot set the initiator as an object unless a new object is created in the initiator.

· Automatic attributes, object initializers, and set initial values

Previously:

private int property;        public int Property        {            get { return property; }            set { property= value; }        }

Now, you can simply enter:

Public int Property {get; set;} // (Note: quick and convenient prop (propfull...) + Tab key)

Object initialization:

UserInfo model = new UserInfo {id = Person. id, name = Person. name, age = Person. age };

(Person. Id is the following anonymous type)

Set initialization:

List<UserInfo> list = new List<UserInfo> {            new UserInfo{age=1,name="Name",id=1},            new UserInfo{age=2,name="Name",id=2},            };

· Extension Method)

This feature saves a lot of code.

Definition: extension method, which allows changing a class, but does not require the source code of the class. The extension method is static. It is a part of the class, but it is not actually put in the class source code. (More references: http://www.cnblogs.com/ldp615/archive/2009/08/07/1541404.html)

Eg:

public static class Extention        {            public static MvcHtmlString WriteHtml(this HtmlHelper htmlHelpder, string html)            {                return new MvcHtmlString(html);            }            public static string toJsons(this object o)            {                JavaScriptSerializer serializer = new JavaScriptSerializer();                return serializer.Serialize(o);            }        }

Call:

Var model = list [0]; string jsonStr = model. toJson (); // call the extension method toJson ()

Result:

{"id":1,"name":"Name","age":1}

Note:

1) if the extension method has the same signature as the method defined in this type, the extension method will never be called.

2). The extension method is put into a range at the namespace level. For example, if you have multiple static classes containing extension methods in the same namespace named Extensions, all these extension methods are put in the range by the using Extensions; command.

Summary: What is the difference between static methods and common static methods. This keyword is added before the first parameter of this method. Extension Method:
1) the class of the method must be static.
2). The method must also be static.
3). The first parameter of the method must be the type you want to extend. For example, if you want to extend a method to int, the first parameter must be int.
4). A keyword "this" is also required before the first parameter.

Anonymous type

The Var keyword is used to indicate implicitly typed variables. When using the var and new keywords together, you can create an anonymous type. The anonymous type is only a class that inherits the Object and has no name. The definition of this class is inferred from initialization, similar to an implicit type variable.

Eg:

 var Person = new { id = 1, name = "yourName", age = 22 };UserInfo model = new UserInfo { id = Person.id, name =Person.name, age =Person.age };

(Note the difference between object initialization and value assignment under the anonymous type)

· ASP. NET AJAX

.

The project is also valuable for the new company that came in March. It is an e-commerce enterprise (Tourism block), but it is a full screen control in the project, super invincible and difficult to maintain, the landlord almost never used controls to write code so disgusted, and that control ajax does not understand ah .. compilation project errors.

FrameWork4.0 features

4.0 with the popularity of mvc, a lot of excellent content, but the landlord has no environment to enjoy, sigh.

See address http://www.cnblogs.com/webabcd/archive/2010/06/29/1767212.html

List the features frequently used by the company's landlords.

· Dynamic type

There are two ways to create your own dynamic object:

1) derived from DynamicObject.

2) use ExpandoObject. DynamicObject requires a lot of work, because several methods must be rewritten, and ExpandoObject is a sealed class that can be used immediately.

Dynamic dyPerson = new ExpandoObject (); // create a dynamic object.

There are two restrictions on dynamic types. Dynamic objects do not support extension methods, and anonymous functions (lmabda expressions) cannot be used as parameters for dynamic method calls.

· Optional parameters and naming Parameters

/** Named parameters and optional parameters * named parameters: when calling a method, you can specify the parameter name to pass the value * optional parameters: when declaring a parameter in a method, you can set the default value for it. When calling this method, this optional parameter can be ignored */

You can see that the default route is used in mvc3.

Eg:

public void FunctionNamedParam(int x, int y , int z=1)

The function call will be:

FunctionNamedParam(x:1, z:3, y:2);FunctionNamedParam(y:1, x:3);

OK. These things are summarized by the landlord while learning while working. They are a bit cool, but after all, they have accumulated a little bit. Of course, there are some missing information. I would like to ask you to add and exchange ideas.

 

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.