Personal notes 1.4-.net 3.0 New Features

Source: Internet
Author: User
I have read several articles from scottgu Article Later, I wanted some new features of C #3.0 to be left on my blog. However, since the environment does not allow the use of. Net 3.0 for a long time, I can't help but feel free to use a keyboard today, because I installed the. NET 3.0 framework SDK on my computer. Here, I will summarize the research achievements of my predecessors, and hope to further consolidate my knowledge for future development. I also hope to bring you some memories.

Next let's take a look at the "Var" keyword of the declaration of variables commonly used in scripts. Its name is "implicit declaration variable". The type of the declared variable is assigned a value (initialized) by the first variable). In my personal opinion, this will decrease Code But some people say that this keyword is mainly designed for LINQ, because the types involved in LINQ may be quite complex or they do not know their types when declaring variables. According to previous experiences, this keyword is only used to spoof us. It is strongly typed during compilation and there is no new content in Il. It is worth noting that the variables declared with VAR must be initialized. If the ry type is initialized, it must also be named. Let's take a look at the example below: 1 VaR num = 0 ; // Num int
2 VaR Nums = New [] {1,2,3,4,5} ; // Nums int []
3 VaR Str = '' ; // STR string
4 VaR list = New List < Int > (); // Int indicates
5 Foreach (VAR I In Nums) {
6Num+ =I;
7}


Another amazing new thing is the extension method. I believe everyone has heard or used this new feature. This is also one of my favorite new features. We can get our own extended methods such as the string type, in addition, you can intelligently prompt when writing code in. In scottgu's article, we know that extension methods must be extended in static classes. For example, if we want to write a method to verify the length of a string, we can write it like this: 1 Namespace Nexplus. csharp3feature
2 {
3 Public Static Class Methodextend
4 {
5 Public Static Bool Outlength ( This String Str) {
6 If (Str. Length > 10 ) Return True ;
7 Return False ;
8 }
9 }
10 }

 If you want to call this function, you can introduce the namespace (using nexplus. csharp3feature;) Then you can call this method in your own method: string. outlength (yourstring); Is it amazing. We all know that object is the ancestor of all classes. What if we extend the object method? You can call your extended methods in all classes. I won't go into details here. If you don't believe it, you can give it a try.

Next, let's take a look at the simplification of object initialization and so on. We don't want to complain about writing entity classes, but we have simplified a lot in. Net 3.0. Let's take a look at it. Public Class Person {
PublicName{Get;Set;}
PublicNickname{Get;Set;}
}

VaR person = New Person {Name="Xiaobing", Nickname="Cyber soul soldier"} ;

VaR persons = New List < Person > {
New Person {Name="Xiaobing", Nickname="Cyber soul soldier"} ,
New Person {Name="Xiaobing1", Nickname="Cyber soul SOLDIER 1"}
};

Isn't it so happy? You don't need to work so hard to write code in the future. Initialization is much simpler. Don't add a new one like 2.0 and then add it. When we enjoy this wonderful feature, lambda expressions are even more exciting, so don't worry about it.

There are a lot of articles about lambda expressions circulating on the Internet, and there are a lot of searches, mainly because it has a good combination with LINQ. This expression is often used in LINQ. In fact, the lambda expression is an upgraded version of the proxy. Let's take a look at other people's examples to see the different code of using the proxy in each version:

Suppose you need to create a button to update the content in ListBox when you click it. In C #1.0 and 1.1, you need to do this:
1 Public Myform ()
2 {
3 ListBox = New ListBox ();
4 Textbox = New Textbox ();
5 Addbutton = New Button ();
6 Addbutton. Click + = New Eventhandler (addclick );
7 }
8 Void Addclick ( Object Sender, eventargs E)
9 {
10ListBox. Items. Add (textbox. Text );
11}

In 2.0, the Code is as follows: 1 Public Myform ()
2 {
3 ListBox = New ListBox ();
4 Textbox = New Textbox ();
5 Addbutton = New Button ();
6 Addbutton. Click + = Delegate
7 {
8ListBox. Items. Add (textbox. Text );
9} ;

if you are not familiar with the proxies in 1.0 and 1.1, you have not learned to use the proxies in 2.0. It doesn't matter if the lambda expressions in 3.0 simplify the proxy. The Code is as follows: 1 Public myform ()
2 {< br> 3
4 addbutton. click => ListBox. items. add (textbox. text);
5 }

Lambda can't tell a few things about it, so it will be applied to this expression in a lot in LINQ. Let's take a look at it later! So far, I think the most important thing to expect is LINQ, and so does it. net 3.0 introduces relational query statements into the object-oriented world, greatly enhancing syntax security. It is also a new revolution for SQL developers. One of my colleagues has this idea: there are also many articles on this aspect of LINQ, powerful syntax expression capabilities,The where, select, group, orderby, and into syntaxes are all supported, and I have not learned this well, but I have not actually used it in general, I heard that a forum is used for Development. I don't know how it works? Here is a brief introduction to LINQ.

The above are the new features that can be learned so far. I don't know what. Net 3.5 is. Microsoft's pace is so fast that I can't catch up with it. Heard a lotProgramStill using 1.1, there are still many companies developing 1.1 programs. I don't know how to think about it, but I have forgotten 1.1, with a mind of 2.0, and will go to 3.0, the ideal future is 3.5. I am at a limited level. I have referred to some materials. If you do not write well, please forgive me. If you think I am pirated, you can leave a message for me :)

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.