Common feature integration for each version of the framework

Source: Internet
Author: User
Tags tojson

· Code Snippet . This feature has long been known, the framework has provided a lot of code snippets, we can also customize the code snippet, but it has not been used before, today practice, or very interesting, this code automatically generated ideas is actually very useful. Also found that in the framework provided in the code snippet, about C # and VB the number is obviously different, do not know why C # less? Surf the Internet and say that MSDN provides extensions to C # code snippets that you can download and experiment with.

· Refactoring. It's always easy to use in eclipse when refactoring through the IDE. Now VS2005 also added this function, feel a bit, and eclipse almost, a better place is to refactor through VS2005, it is very useful to keep different items in the same solution in sync.

· Debug . VS2005 can support debug code modification, this feature has been used very frequently.

· nullable type . For Boolean types, you can specify Ture or false for him. Please see the following code

public void Test ()            {                int? i = null;                Int? y = 4;                Int? x = i + y;                Console.Write (x);//x is null              }

· The empty merge operator . The empty merge operator provides a quick way to express the possible value of NULL when working with nullable types and reference types.

1). If the first operand is not NULL, then the entire expression equals the value of the first operand.

2). If the first operand is null, then 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 not available. Here, borrow it first.

Partial classes (partial class)

There's nothing to say about breaking a class into two classes, but the class name is the same. Control initialization that is common to. NET is written in some classes.

Public partial class Test {//<summary>///        Head1 controls. ///</summary>///<remarks>///        Auto-generated fields. ////        to make changes, move the field declaration from the designer file to the code-behind file. //        </remarks>        protected global::system.web.ui.htmlcontrols.htmlhead head1;//this line}

Paste this line of code into a. cs file, and the program will run as well.

FrameWork3.5 characteristics

3.5 of the features in the daily work, intentional/unintentional also used more. The following one by one lists

This is a link http://weblogs.asp.net/scottgu/archive/2007/11/19/visual-studio-2008-and-net-3-5-released.aspx I found when I was looking for a version feature.

This article describes the new features of version 3.5.

. Linq

There are too many people in this discussion. It's all over.

· Lambda

Starting with c#3.0, you can use a new syntax to assign the implementation code to a delegate: a lambda expression.

A lambda expression can be used wherever there is a delegate parameter type.

The required parameters are listed on the left side of the lambda operator "=". The right side of the lambda operator defines the implementation code for the method that is assigned to the lambda variable.

Public delegate string thisaction (string par);        static void Main (string[] args)        {            Thisaction lambda = a + = {return a + = "This is my Test";};        It is understood here that anonymous functions are used? Generally speaking, it should be thisaction lambda=functionname.            string aa = Lambda ("Hi,");            Console.Write (AA); }

The following:

  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";        }

It should be so, the hand knocked not to test it.

Just one is to put logic in a lambda, a method encapsulates the next, and then directly assign the method name to the delegate. Oh, yes, that's where the comment is, it should not be an anonymous function. It's just a way of writing lambda support.

So it can be seen from above that many people use the first method to write logic into a lambda without encapsulating the logic in a method and then invoking the method name. Clear your mind and look at the code that someone else has written.

(In fact, I've always understood that Lambda is the place where the collection is used.) But always like to confuse. )

· Var implied type variable

Type inference, the compiler infers the type of the variable based on the initial value of the variable.

Rules to follow:

1) variables must be initialized. Otherwise, the compiler does not infer the basis of the variable type.

2) The initializer cannot be empty.

3) The initializer must be in front of an expression.

4) The initializer cannot be set to an object unless a new object is created in the initializer.

· Automatic attributes, object initializers, and collection initial values

before:

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

Now it's time to abbreviate:

public int Property {get; set;} (Note: Quick and easy way prop (Propfull ...) +tab key +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 methods (Extension method)

This feature can save a lot of code if it is used well.

Definition: An extension method that allows a class to be changed, but does not require the source code of the class. The extension method is static and is part of the class, but is not actually placed in the source code of the class. (More reference: 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) c7/>{                return new mvchtmlstring (HTML);            }            public static string Tojsons (This object o)            {                JavaScriptSerializer serializer = new JavaScriptSerializer (); C13/>return Serializer. Serialize (o);            }        }

Call:

var model = list[0];            String jsonstr = Model.tojson ();  The extension method called Tojson ()

Results:

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

Attention:

1). If the extension method has the same signature as the method defined in the type, the extension method is never called.

2). The extension method is placed in the scope at the namespace level. For example, if you have multiple static classes in the same namespace named Extensions that contain extension methods, then these extension methods will all be made by using Extensions; instruction into the range.

Summary : What is the difference between a normal static method and a common one? This keyword is preceded by the first parameter of this method. Extension methods:
1). The class in which the method resides 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, then the first argument must be int.
4). A This keyword is also required before the first parameter.

Anonymous type

The Var keyword, which is used to represent implicitly typed variables, allows you to create an anonymous type when the Var and new keyword are used together. An anonymous type is simply a class that inherits from object and has no name. The definition of the class is inferred from initialization, similar to implicitly typed variables.

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 an anonymous type and an object initialization assignment)

· ASP. NET AJAX

Slightly.

Landlord Spit Trough, August to the new company, this company project also has value bar, is an e-commerce enterprise (tourism block), but the project inside full-screen control, super invincible difficult to maintain, landlord almost useless control write code good Offensive Ah, and that control Ajax can not understand AH. Compile the project various error.

FrameWork4.0 characteristics

4.0 with the hot of MVC, a lot of fine content, but the landlord did not enjoy the environment, bemoaned sigh.

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

In the company of the landlord commonly used characteristics listed under.

· Dynamic type

There are two ways to create your own dynamic objects:

1) derive from DynamicObject.

2) Use ExpandoObject. Using 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 Live object.

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

· Optional parameters and named parameters

You can refer to MVC3, which is used in the default route.

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, it's over. These things are the landlord side of the work while learning their own summary, take out some shabby, but after all, it is a little accumulation of their own. Of course there are gaps, but also urge you to add, mutual exchange.

Common feature integration for each version of the framework

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.