C # methods, properties, and events

Source: Internet
Author: User

1.7.3 method
A method is a member that executes a calculation or behavior that can be done by a pair of images or classes. The method has a formal parameter list (which may be empty), a return value (or void), and can be static or non-static. Static methods are accessed through classes. A non-static method, also known as an instance method, is accessed through an instance of the class.

1 usingSystem;2  Public classStack3 {4  Public Staticstack Clone (stack s) {...}5  Public Staticstack Flip (stack s) {...}6  Public ObjectPop () {...}7  Public voidPush (Objecto) {...}8  Public Override stringToString () {...}9 ...Ten } One classTest A { - Static voidMain () { -Stack s =NewStack (); the  for(inti =1; I <Ten; i++) - S.push (i); -Stack flipped =Stack.flip (s); -Stack cloned =Stack.clone (s); +Console.WriteLine ("Original Stack:"+s.tostring ()); -Console.WriteLine ("Flipped Stack:"+flipped. ToString ()); +Console.WriteLine ("Cloned Stack:"+cloned. ToString ()); A } at}

Describes the stack, which has many static methods (clone and Flip) and many instance methods (Push, Pop, and ToString).
Methods can be called repeatedly, which means that multiple methods may have the same name as long as there is a unique signature. The signature of a method includes the names of the various types of methods, data, modifiers, and its formal parameters. The signature of the method does not include the return type.

1 classTest2 {3     Static voidF ()4     {5Console.WriteLine ("F ()");6     }7     Static voidFObjecto)8     {9Console.WriteLine ("F (object)");Ten     } One     Static voidFintvalue) A     { -Console.WriteLine ("F (int)"); -     } the     Static voidFintAintb) -     { -Console.WriteLine ("F (int, int)"); -     } +     Static voidFint[] values) -     { +Console.WriteLine ("F (int[])"); A     } at     Static voidMain () -     { - F (); -F1); -F ((Object)1); -F1,2); inFNew int[] {1,2,3 }); -     } to}

Describes a class that has a member method F. The output of the program is

1 F () 2 f (int)3 F (object)4 f (intint )5 F (int[])

1.7.4 Property
A property is a member that provides access to an attribute of an image or class. Examples of attributes include the length of the string, the size of the font, the focus of the window, the user's name, and so on. A property is a natural extension of a domain. Both are named with the relevant type member, and the syntax for accessing the domain and property is the same. However, unlike domains, properties do not indicate where to store. As an alternative, a property has an accessor that specifies the execution of the declaration to read or write to them.
Properties are defined by the property declaration. The first part of the property declaration looks quite similar to the domain declaration. The second part includes a Get access program and a set accessor. In the following example, the class button defines a caption property.  

1  Public classButton2 {3     Private stringcaption;4      Public stringCaption5     {6         Get7         {8             returncaption;9         }Ten         Set One         { ACaption =value; - Repaint (); -         } the     } -}

Properties such as read-write properties like the Caption property include the get and set accessor programs. The get accessor is called when the value of the property is to be read, and the set accessor is called when the property value is to be written. Properties in the set accessor, the new value of the property is assigned to an implied parameter named value.
The declaration of a property is relatively straightforward, but the property explicitly has its own value when it is used rather than at the time of the Declaration. You can read and write caption properties by reading and writing to the domain:

1 New Button (); 2 " ABC " // Set 3 string // Get 4 " DEF ";//Get & Set

1.7.5 Events
An event is a member that makes a notification to the like and class. A class defines an event by providing an event declaration, which looks quite similar to the domain and event declaration, but has an event keyword. The type of this declaration must be of type delegate.
In this case,

1  Public Delegate voidEventHandler (Objectsender, Event e);2  Public classButton3 {4      Public EventEventHandler Click;5      Public voidReset ()6     {7Click =NULL;8     }9}

The button class defines a Click event of type EventHandler. In the button class, the Click member corresponds to a private domain of type EventHandler. However, outside the button class, the Click member can only be used to the left of the + = and-= operators. This restricts the client code in terms of adding and removing event handles. Example

1 usingSystem;2  Public classForm13 {4  PublicForm1 () {5 //Add Button1_Click as an event handler for Button1 ' s Click event6Button1.Click + =NewEventHandler (button1_click);7 }8Button Button1 =NewButton ();9 voidButton1_Click (Objectsender, Event e) {TenConsole.WriteLine ("Button1 was clicked!"); One } A  Public voidDisconnect () { -Button1.Click-=NewEventHandler (button1_click); - } the }

Describes the class Form1, which adds button1_click as an event handle for the Button1 click event. In the Disconnect method, the event handle is removed.
As shown in the example, the class button needs to be overridden to use an event declaration like a property, rather than a domain-like event declaration.

1  Public classButton2 {3  Public EventEventHandler Click {4 Get {...}5 Set {...}6 }7  Public voidReset () {8Click =NULL;9 }Ten}

This change does not affect the customer code, but because the click event handle does not need to be implemented with a domain, the execution of the class button is allowed to be more flexible.  

Transfer from http://blog.csdn.net/chenyuling/article/details/1569590

C # methods, properties, and events

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.