Some skills in .NET programming (1)

xiaoxiao2021-03-06  42

Lazy_allocate it allows objects to save some memory costs

Public Class Myclass

{

Private dataset_ds;

Public DataSet DS

{

get

{

If (_ds == null) // If you do not create an instance, create a new one

{

_ds = new dataset ();

}

Return_ds;

}

}

}

The Static Helper Object Sealed keyword can prevent class from inherit .STATIC can define a static variable or static function.

Public Sealed Class MyString

{

Public static string getHello ()

{

Return "Hello World!";

}

}

This can be obtained by mystring.getHello () "Hello World!" Helper Object's central concept is to package common auxiliary functions into static functions, and designers do not need to repeatedly write these program code Eventc #. Event is closely related to delegate. Delegate translation is delegate, in fact, is a function pointer. The following code is the simplest application example of Delegate, which will output Hello World in the browser side!

Public delegate void mydele (string a);

Public class delegateTest: System.Web.ui.page

{

Private Void Page_Load (Object Sender, System.EventArgs E)

{

Mydele md = new mydele (output);

MD ("Hello World!");

}

Public void Output (String a)

{

Response.write (a);

}

#Region web form designer generated code

Override protected void oninit (Eventargs E)

{

//

// Codegen: This call is necessary for the ASP.NET Web Form Designer.

//

InitializationComponent ();

Base.onit (E);

}

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

}

The EVENT keyword enables you to specify the delegation when certain "events" in the code occurs. This delegate can have one or more associated methods, and when the code indicates that the event will call the associated method. The events in a program can be used to run other programs for the .NET Framework public language runtime. In order to create and use C # events, the following steps must be taken:

Create or identify a delegate. If you are defining your own event, you must ensure that there is a delegate with the event keyword. If events have been predefined (eg, in .NET Framework), the user only needs to know the name of the commission. Create a class, including:

Events created from the commission. (Optional) Verify that the method of the delegate instance declared with the Event keyword. Otherwise, the logic must be placed in the code that triggers this event. Method of calling this event. These methods can override some basic class functions. Such definition events. Define one or more classes that connect methods to events. All these classes include:

Use the = operator and - = operator to associate one or more methods with events in the base class. The definition of the method associated with the event. Use this event: Create a class object that contains event declarations. Use the defined constructor to create a class object that contains event definitions. The sample code is adapted from MSDN:

Using system;

Namespace mycollections

{

Using system.collections;

// Define commission

Public Delegate Void ChangeDeventHandler (Object Sender, Eventargs E);

// This type of function is the same as ArrayList, but adds an event when its content is modified.

Public Class ListwithchangeDevent: arraylist

{

/ / Define the name of the event

Public Event ChangeDeventhandler Changed;

// internal method, each internal modification method calls it

Protected Virtual Void Onchanged (Eventargs E)

{

IF (Changed! = NULL)

Changed (this, e);

}

// override the method of each class's internal modification to trigger the event when modified

Public override int Add (Object Value)

{

INT i = base.add (value);

ONCHANGED (Eventargs.empty);

Return I;

}

Public override void clear ()

{

Base.clear ();

ONCHANGED (Eventargs.empty);

}

Public Override Object this [int index]

{

set

{

Base [index] = value;

ONCHANGED (Eventargs.empty);

}

}

}

}

Namespace TESTEVENTS

{

Using mycollection;

Class EventListener

{

Private ListwithChangedendevent List;

//Constructor

Public EventListener (ListwithChangedevent List)

{

List = list;

/ / Add a list of a list of a List to call the ListChanged method when triggering

List.changed = new changeeventhandler (listchanged);

}

/ / ListChanged method called when triggering

Private Void ListChanged (Object Sender, Eventargs E)

{

Console.writeline ("this is caled when the evenet fire");

}

Public void detach ()

{

// will be separated from the method associated with the event and remove the list from memory

List.changed - = new changedeventhandler (listchanged);

List = NULL;

}

}

Class test

{

Public static void main ()

{

// Create an instance of ListwithChangeDevent

Listwithchangedevent list = new listwithchangedevent ();

// Create an instance of the listening class of the LIST

EventListener Listener = New EventListener (List);

// List performs the operation of adding and emptying items

List.add ("Item 1"); // Trigger a Changed event list.clear (); //) 触 事d 事 这里

Listener.detach ();

}

}

}

The following is the code explanation given by MSDN: Declaring events to declare events within the category, first, you must first declare the type of commission (if there is no declaration). Public Delegate Void ChangeDeventHandler (Object Sender, Eventargs E); a set of parameters that pass to the method of processing the event. Multiple events can share the same delegate type, so this step is only required when any suitable commission type has not yet been declared. Next, declare the event itself. Public Event ChangeDeventHandler Changed; Declare the event is similar to the field of the declaration, just the keyword Event, in front of the event declaration, behind the modifier. Events are usually declared as public events, but allow any access to modifiers. After the event class declares the event, it can process the event as a field indicated by the delegated type. If no client will entrust with the event, this field will be empty; otherwise the field reference should be invoked when calling the event. Therefore, when you call an event, you will usually check if it is empty, then call the event. if (Changed! = NULL) Changed (this, e); calling events can only be done from within the class that declares the event. Look with the event hook from the category of statement, the event is like a field, but access to this field is very limited. Only the following operations: written new delegates on this field. Remove the delegate from the field (which may be a composite field). Use the = and - = operator to complete this. To start receiving event calls, customer code first creates a delegate of event types, which should be called from event calls. Then it uses = to write the delegate to any other delegation that may be connected.

// Add "listchanged" to the change Event on "list":

List.changed = new changeeventhandler (listchanged);

When the customer code completes the receiving event call, it will use the operator - = from the event to remove its delegate.

// Detach The Event and delete the list:

List.changed - = new changedeventhandler (listchanged);

There is an intermediate EventListener class in the sample code of MSDN, which seems to be unhealthy, I wrote one, it is difficult to provide simpler than the MSDN:

Namespace DesignPattern

{

///

/// Eventtest's summary description.

///

USING MYNS;

Public class eventtest: system.Web.ui.page

{

Private Void Page_Load (Object Sender, System.EventArgs E)

{

Data D = New Data ();

D.Added = New ChangedHandler (this.Add);

D.Reloaded = New Changdhandler (this.Reload);

D.Add (5);

D.Reload ();

}

Private Void Add (Object Sender, System.EventArgs E)

{

Response.write ("Add finish
);

Private Void Reall (Object Sender, System.EventArgs E)

{

Response.write ("Reload Finish
);

}

#Region web form designer generated code

Override protected void oninit (Eventargs E)

{

//

// Codegen: This call is necessary for the ASP.NET Web Form Designer.

//

InitializationComponent ();

Base.onit (E);

}

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

}

}

Namespace myns

{

Public Delegate Void ChangedHandler (Object Sender, System.EventArgs E);

// A super simple data class, only one int variable, the class is only the simplest addition to zero 2 methods, defined 2 events

Public Class Data

{

Private INT i = 0;

Public Event Changedhandler Added;

Public Event Changedra Reloaded;

Protected Void Onadd (Eventargs E)

{

IF (Added! = NULL) Added (this, e);

}

Protected Void OnreLoad (Eventargs E)

{

IF (Reloaded! = NULL) Reloaded (this, e);

}

Public void add (int key)

{

i = key;

ONADD (Eventargs.empty);

}

Public void reload ()

{

i = 0;

OnreLoad (Eventargs.empty);

}

Public int getnum ()

{

Return I;

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-54745.html

New Post(0)