C #3.0 new experiences (1) Implicit local variables

Source: Internet
Author: User

[Transfer] http://blog.csdn.net/maotin/archive/2008/09/23/2968141.aspx

C #3.0 new experiences (1)

Maotin

20080923

Preface Overview:
Microsoft has been moving fast and often finds that many technologies are out of date (probably because I have low learning ability). As a developer, I think it should be at the forefront of technology, be sure to keep up with the pace of new technologies (there is no way to keep up with these things and you will not be able to afford them for a while );
I have been using vs.net (C #) for development since 2002 and can be used in computing. net is an old programmer. net does not know much about it, or has a half understanding; (sweat such as spring) as a use for 5 to 6 years. NET development programmers actually only know that the page control should be neat and nice, and the UI data should be passed to the business layer for processing, and then the data layer access should be called (the main work is to process the content ), for a long time, I don't know why Asp.net (I used to do winform), the most primitive development and use. net comes with IDE tools, no auxiliary tools are developed, there is no concept of architecture design, project management does not know how to start, testing depends on manual interface, performance optimization has no effect, data transactions are not guaranteed, installation and deployment never know how to do it, code refactoring never done, design mode only know factory ......
There are too many things I don't understand. (There are too many content included in the ellipsis) I 've been in touch with Asp.net development in the past two years. I 've been exploring a lot of detours, I feel that a lot of basic things are new. When I have doubts about my own capabilities, I am excited about Microsoft (and of course other Web technology vendors, it's okay to develop so many technologies, so how can I live? (poor ability, lazy personality type), learn a technology (point), and there are n + 1 in the back;
Over the past two years, I have been busy decorating my daughter-in-law (all excuses for laziness). The old technical skills I have accumulated are outdated and many of them cannot be used. This has directly led to a serious consequence: I have not raised my salary in the past two years, and I have to be careful about my unemployment problem (an old man or a small man, and an evil bank loan oppression). under a lot of pressure, I decided to make a thorough transformation, to be a new person, and to fight for release as soon as possible, we should say that we should wash our hands in the golden basin (saving 0.8 million yuan, of course, we need to calculate the dollar, this society does not need to spend RMB). Therefore, we decided to start this C #3.0 course from basic learning, we hope that you will have some help with the materials you have prepared!

I. C #3.0 main language enhancements

• Implicit local variables

• Extension Method

• Object and set Initiator

• Anonymous type

• Lambda expressions

• Query expression (LINQ)

• Expression Tree

Ii. Implicit local variables

Let's take a look at the example:

VaR I = 5;

VaR H = 23.56;

VaR S = "C sharp ";

VaR intarr = new [] {1, 2, 3, 4 };

VaR A = new [] {1, 10,100,100 0 };

Is the keyword "Var" very familiar? It is often used in JavaScript (mutual learning is common now). Of course, the differences between VaR and Javascript in C # are still relatively large.

1. var can only be used as a local variable;

That is to say, VaR can only be defined in one method, or in statements such as foreach, for, and using:

Public class vartest
{
Private void test ()
{
VaR I = 0;
}
}

2. var can declare any type variable and can automatically infer the type according to the subsequent initialization statement;

This is a bit like an object, but VaR is a strong type. The variable type is defined according to the initialization expression;

VaR I = 0; I is int type

VaR STR = "test"; STR is of the string type.

3. Expression Initialization is required when VaR is defined;

That is, it must be defined as VAR I = 0. If no initialization compilation is performed, an error will be reported, and the initialization value cannot be null. the compiler cannot deduce the type of a Variable Based on null. The initial value except int, in addition to data types such as string, you can also use a new object as the initial value. (according to the comments posted on the Internet, the most common use of VaR is to use it with an anonymous object, and use it in LINQ)

// Below is a blog written by a netizen on the Internet. Copy and modify it!

Use with the anonymous type:

 

VaR A = new {name = "maotin", age = 99 };
Console. writeline (A. Name );
Console. writeline (A. Age );

 

Applications in LINQ:

 

Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
VaR querylownums =
From num in numbers
Where num <5
Select num;
Foreach (VAR s in querylownums)
{
Console. Write (S. tostring () + "");
}

 

4. var usage suggestions

Generally, VaR is not used like "Var I = 9;", and the program is not highly readable;

It is more convenient to use the anonymous type; it is also good to use in foreach;

VaR is just a language-level thing. The compiler will convert it into a real type through type inference during compilation.

(Please indicate the source of the post! Thank you)

 

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.