Deep understanding of the five major improvements in C # 3.0

Source: Internet
Author: User
Tags extend static class

1. Implicit local variables

C # 3.0 introduces a new keyword called "var". This keyword allows the developer to create a variable, but does not have to be explicit about its type. For example, use Var to describe a string, like this:

var myData = "This is my data";

Note that there is no mention that the MyData variable is a string, and C # 2.0 asks for it.

Although Var allows you to create an implied type, it does not degrade C # 's strong type characteristics. The VAR keyword is only useful when creating a variable, and once you have established the variable and determined its type, you cannot use Var to change the type of the variable.

For example, this code does not work:

var myDate = DateTime.Now;
myDate = "Hello.";

Using the VAR keyword also produces an interesting result that can help developers reduce the code input when creating variables. For example, to create a customer object in C # 2.0, you would enter the following code:

Customer myCustomer = new Customer();

With the new var keyword, simply enter:

var mycustomer = new Customer ();

Another feature of the Var keyword is that it avoids changing a method call that returns a type object. For example, in C # 2.0, if you need to call a method that returns a Customer object, you should write the following code:

Customer myCustomer = GetByName("Zach");

If a Getbyname method returns an object that is not a customer at some point, the code cannot be compiled. However, if you apply the var keyword, you do not have to worry about the type of object returned by Getbyname.

var myData = GetByName("Zach");

Now, because the VAR keyword is applied, the Getbyname method can be changed to return a person object, and this method call is still valid.

2.extension method

In C #, you cannot inherit and extend a type marked as "encapsulated" with an access identifier. But in C # 3.0, the extension method allows you to extend any class, even a class that is marked as encapsulated. For example, if you want to add a nospaces () method to a string class, we want to define a extension method similar to the one in List A.

List A

namespaceMyExtensionMethods
{
 public static class Extension
 {
  public static void NoSpaces(this string data)
  {
   return data.Replace(" ", "");
  }
 }
}

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.