C #3.0 new feature-Extension Method

Source: Internet
Author: User

From http://www.cnblogs.com/jiejie_peng/archive/2010/04/08/1707195.html

C #3.0 new feature-Expansion Method
  1. Main Purpose
    Measure the test taker's knowledge about the extension of a compiled class method.
  2. Special note
    A. When a class is extended, the new class must be modified using public static because the default class is the internal access modifier.
    B. This class must have a file scope, that is, it cannot be used as a nested class.
    C. Declare the extension method as static
    D. The first parameter type of the extension method is the class to be extended, and this keyword must be added before the Parameter
    E. the extension class name is very important. When the Net Framework class library is updated, if the updated class library contains the same class name as the static extension class you previously defined, the custom static extension class is hidden.
  3. Implementation
    As follows: Code Add a method to extend the object class. Because all classes in. Net inherit from the object class, you can select the int type at will during the call.
    The Code is as follows:
     1   Public   Static   Class Extendobject
    2 {
    3 Public Static Void Showtypeinfo ( This Object OBJ)
    4 {
    5 Console. writeline ( " My datatype is {0} " , Obj. GetType (). Name );
    6 }
    7 }

    The code for calling is as follows:
    Two call methods are available: instance call and extension class name call.

    Code
     1   Class  Program
    2 {
    3 Static Void Main ( String [] ARGs)
    4 {
    5 Int A = 192 ;
    6 // Call static methods using class instances
    7 A. showtypeinfo ();
    8
    9 // Call static methods using static class names
    10 Extendobject. showtypeinfo ();
    11 Console. Read ();
    12 }
    13 }

     
    Note that the extension class and the extended class are two different classes. Therefore, when class methods in the extension class access the members of the extended class, only members declared as public modifiers can be accessed.

    The Code is as follows:

    Extended class: Bird

     
    ClassBird
    {
    Private String_ Btype;
    Public StringBtype
    {
    Get;
    Set;
    }
    }

    Extended extendbird

     1   Public   Static  Class  Extendbird
    2 {
    3 Public Static Void Setbirdtype ( This Bird B, String Birdtype)
    4 {
    5 // Public attribute, accessible
    6 B. btype = Birdtype;
    7
    8 // Compilation errors may occur.
    9 _ Btype = Birdtype;
    10 }
    11 }

 

  1. Main Purpose
    Measure the test taker's knowledge about the extension of a compiled class method.
  2. Special note
    A. When a class is extended, the new class must be modified using public static because the default class is the internal access modifier.
    B. This class must have a file scope, that is, it cannot be used as a nested class.
    C. Declare the extension method as static
    D. The first parameter type of the extension method is the class to be extended, and this keyword must be added before the Parameter
    E. the extension class name is very important. When the Net Framework class library is updated, if the updated class library contains the same class name as the static extension class you previously defined, the custom static extension class is hidden.
  3. Implementation
    The following code adds a method to extend the object class. Because all classes in. Net inherit from the object class, the int type is randomly selected during the call.
    The Code is as follows:
     1   Public   Static   Class Extendobject
    2 {
    3 Public Static Void Showtypeinfo ( This Object OBJ)
    4 {
    5 Console. writeline ( " My datatype is {0} " , Obj. GetType (). Name );
    6 }
    7 }

    The code for calling is as follows:
    Two call methods are available: instance call and extension class name call.

    Code
     1   Class  Program
    2 {
    3 Static Void Main ( String [] ARGs)
    4 {
    5 Int A = 192 ;
    6 // Call static methods using class instances
    7 A. showtypeinfo ();
    8
    9 // Call static methods using static class names
    10 Extendobject. showtypeinfo ();
    11 Console. Read ();
    12 }
    13 }

     
    Note that the extension class and the extended class are two different classes. Therefore, when class methods in the extension class access the members of the extended class, only members declared as public modifiers can be accessed.

    The Code is as follows:

    Extended class: Bird

     
    ClassBird
    {
    Private String_ Btype;
    Public StringBtype
    {
    Get;
    Set;
    }
    }

    Extended extendbird

     1   Public   Static  Class  Extendbird
    2 {
    3 Public Static Void Setbirdtype ( This Bird B, String Birdtype)
    4 {
    5 // Public attribute, accessible
    6 B. btype = Birdtype;
    7
    8 // Compilation errors may occur.
    9 _ Btype = Birdtype;
    10 }
    11 }

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.