2008 initial test of VB.net Extension Method

Source: Internet
Author: User
Extension Method ( Extension methods) Is a static method that can be called through the syntax of the instance method. In terms of the final effect, the extension method makes it a reality to expand an existing type and construct a type with an additional method. (Note: Only 3.5 is available)

1 Imports system. runtime. compilerservices
2 ''' <summary>
3 ''' even extension class
4 ''' </Summary>
5 ''' <remarks> </remarks>
6 Module myextension
7''' <summary>
8 ''' returns a string of a certain length.
9 ''' </Summary>
10''' <Param name = "str"> input string </param>
11''' <Param name = "len"> return length </param>
12''' <returns> </returns>
13 ''' <remarks> </remarks>
14 <extension ()> _
15 public function getstring (byval STR as string, byval Len as integer) as string
16 return left (STR, Len)
17 end Function
18end Module

Call:

1 module module1
2
3 sub main ()
4 dim STR as string = ""
5 console. writeline (Str. getstring (12 ))
6 end sub
7
8end Module

The extended method getstring is added to the string class.

Running effect:

 

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.