Chap05 C # advanced type

Source: Internet
Author: User
ArticleDirectory
    • For delegated operations (+,-), multicast delegation:
    • Anonymous delegation:
    • Declaration method:
    • Enumeration type as a bit flag
1. Delegate

Refer to the built-in vs2010 example.

For the delegate operation (+,-), multicast delegate: anonymous delegate:

 

2. Events
 
1. define a string list class (inherited from list <string>) 2. define delegation 3. define events based on delegation 4. define the method of event triggering 5. define the event processing function 6. subscription
 Namespace  Myevent {  Class  Program {  Class Stringlist: List < String > {  Public  Delegate   Void  Dele ();  Public   Event Dele changed; //  Event definition (with delegation)              Public   New   Void Add ( String Item) //  Use new to hide methods in the base class  {  Base . Add (item );//  Call methods in the base class Changed (); //  Event-triggered  }}  ///   <Summary>          ///  Event processing functions  ///   </Summary>          Static   Void  Listchanged () {console. writeline (  " Listchanged_add  "  );}  Static   Void Main ( String  [] ARGs) {stringlist names = New  Stringlist (); names. Changed + = Listchanged; //  Event subscription Names. Add ( "  Tom  " ); Names. Add (  "  Jim  "  ); Names. Add (  "  Lily  "  );}}}
3. Structure-C-language gifts
    • Fields, attributes, and methods that can be declared in struct.
    • The structure is the value type, and the class is the reference type.
4. sealing and Division
    • Sealed class: class that cannot be inherited
    • Partial class: place a long class in different files.
5. Enumeration type declaration method:

Enum days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

Enum months: byte {Jan, Feb, MAR, APR, May, Jun, Jul, Aug, SEP, Oct, Nov, Dec };

By default, the basic type of each element in the enumeration is int. You can use a colon to specify another Integer type, as shown in the preceding example.

Enumeration type as a bit flag
 [Flags]  Enum  Days2 {none = 0x0  , Sunday = 0x1 , Monday = 0x2  , Tuesday = 0x4  , Wednesday = 0x8  , Thursday = 0x10  , Friday = 0x20  , Saturday = 0x40  }  Class Myclass {days2 meetingdays = Days2.tuesday | Days2.thursday ;} 

The following operators can be used: Or (|), XOR (^), and (&).

 //  Initialize with two flags using bitwise OR. Meetingdays = days2.tuesday | Days2.thursday;  //  Set an additional flag using bitwise OR. Meetingdays = meetingdays | Days2.friday; console. writeline (  "  Meeting days are {0}  " , Meetingdays );  //  Output: meeting days are Tuesday, Thursday, Friday  //  Remove a flag using bitwise XOR. Meetingdays = meetingdays ^ Days2.tuesday; console. writeline (  "  Meeting days are {0}  "  , Meetingdays );  //  Output: meeting days are Thursday, Friday  // Test Value of flags using bitwise AND.  Bool Test = (meetingdays & days2.thursday) = Days2.thursday; console. writeline (  "  Thursday {0} A meeting day.  " , Test = True ? "  Is  " : "  Is not  "  ); //  Output: Thursday is a meeting day. 

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.