C #3.0 Learning (1)-implicit type local variables and extension methods

Source: Internet
Author: User
Document directory
  • Implicit type local variable
  • Extension Method
Implicit type local variable

Implicit local variables are declared using the VaR keyword, as follows:

var i = 123;var h=123.123;var s = “oec2003";var intArr = new[] {1,2,3,4} ;var a = new[] { 1, 10, 100, 1000 };

At first glance, it looks a bit like the declaration method in Javascript. Although the keywords are the same, they are essentially different.
After a variable declared with the VaR keyword in C #3.0 is assigned a value, the compiler will automatically deduce the type of the variable according to the type of the variable value during compilation. So it is still a strong type, which is different from the object. In fact, the VaR keyword is not a specific type, but serves as a placeholder. After compilation, It will be replaced with the corresponding type. Note that the variable declared with VAR must be assigned an initial value. Otherwise, a compilation error occurs, because the variable type cannot be inferred based on the value if no value is assigned. VaR can only declare local variables and can be used in foreach, for example:

var nums = new[] { 1, 2, 3, 4, 5 };foreach (var i in nums){}
Extension Method

This is a very useful feature. The extension method allows us to add existing instance methods without changing the source code. The class of the extension method must be a static class. As follows:

public static class oec2003Extensions{    public static bool IsValidEmail(this String s)    {        Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");        return regex.IsMatch(s);    }}

The above isvalidemail static method is in the static class oec2003extensions class. This class can reference the namespace in any namespace.
The isvalidemail method is used to verify email. The method has three parameters: This string S. This is just a compilation requirement. As a prompt, it tells the compiler that this method may be used as an extension method; string is the type we need to extend; s indicates the content of the email to be verified. Let's take a look at how to use this extension method.

protected void Button2_Click(object sender, EventArgs e){    if (this.TextBox1.Text.Trim().IsValidEmail())    {        Response.Write("email is right");    }    else    {        Response.Write("email is error");    }}

Is it amazing that the string type has an added isvalidemail method, which can be called directly to verify the email address.

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.