. Net learning experience

Source: Internet
Author: User

I. C # is a set of Type declarations.

(C Programs are a group of functions and data types, and C ++ programs are a group of functions and classes)

The types here refer to type, including: (1) pre-defined types: int, bool, String, etc.; (2) user-defined types: Class, structure (struct), array, Enum, Delegate, interface, and generic.

Type is created through the type declaration, including:

(1) Type of the type to be created; (2) Name of the new type; (3) Declaration of each member of the type (name and specification ). Except for the array and delegete types, they do not contain named members.

Once the types are declared, they are the same as the predefined types.

2. The memory allocated for declaring a class type variable is used to save the reference (stored in the stack), rather than to save the actual data of the Class Object. To allocate memory for the actual data, use the new operator. For example, myclass MC = new myclass (); here, myclass MC is declaration, while new myclass () is initialization. Similarly, in int I = 2, int I is Declaration, = 2 is initialization.

 

Ii. var keyword.

The VaR keyword can only be used in local variables, that is, only in a method or function. (Fields that cannot be used in the class)

It can only be used when the variable Declaration contains initialization. Because. Net can deduce the VaR type based on the subsequent initialization content. If it is not initialized later, it will not work.

Once the type is inferred, it cannot be changed. It is still a strong type.

The. NET keyword VaR is different from the VaR in Javascript. js can reference different types.

 

3. Stack

Stack is a memory array, which is the data structure of the back-in-first-out mode. Heap: it is a memory area. In the heap, a large block of memory can be allocated to store certain types of data.

Value Type. data is stored in the stack. Reference Type: The first section is a reference pointing to the location where data is stored in the heap. The second section is the actual data, it is always in the heap.

Value types include:

(1) pre-defined types: Byte/INT/long/bool, except object and string.

(2) custom type: struct/enum. Except: Class/interface/Delegate/array.

Therefore, for a method such as void myfun (myclass F1, int F2) {f1.hisint + = 1; F2 + = 1 ;}, in the end, because the parameter is a copy of the variable, one is reference and the other is value type. For value type, changes in the content do not affect the external fields. hisint is a reference type, so the value in the heap outside is changed.

 

Iv. Attributes

The attribute surface is very similar to the "field" in the class. It is a class member and has a type, and can be assigned and read.

However, an attribute is a function member and does not allocate memory for data storage! It executes the code.

The attribute is a set of two matching methods (get/set) that become accessors ).

Class myclass <br/>{< br/> private int therealvalue = 10; // field: Memory Allocation <br/> Public int myvalue // attribute: unallocated memory <br/>{< br/> set // is actually a method: void set (value ), <br/>{< br/> therealvalue = value> 100 <br/>? 100 <br/>: value; // when values are assigned in the future: myclass1.myvalue = 5, it is equivalent to executing the Set (5) method <br/> // The value here is not required, in addition, the method body can write any statement <br/>}< br/> Get // actually a method of the same return type as the attribute: int get (), no parameter <br/>{< br/> return therealvalue; // The return here is required, and you can even write return 1; <br/>}< br/>}

 

Only get accessors are called read-only attributes, and only set accessors are called write-only attributes, but they must be defined at least.

 

V. Delegation and events

(1) delegate is a type that contains an ordered list of methods with the same signature and return value type. When called, he calls every method in the list.

For example:

Delegate void MYDEL (int x );

MYDEL delvar1 = new MYDEL (myobj. mym1 );

MYDEL delvar2 = myobj. mym2 ;//The above is the same as the meaning of this line, but this line omitted new

MYDEL delvar3 = delvar1 + delvar2; // orDelvar1 + = myobj. mym2;

Here, if you run: delvar1 (5), it is equivalent to executing: myobj. mym1 (5); myobj. mym2 (5 );

(2) events are members rather than types, so:

A. Because it is not a type, the new expression cannot be used to create its object.

B. Because it is a member, the object must be declared in the class or structure and cannot be declared in another executable code.

C. Event members are implicitly automatically initialized to null.

It can be said that an event is an encapsulated delegate and a variable of the delegate type.

The name of the delegate type required for the event declaration, for example:

Class mytimerclass

{Public event eventhandler myeve ;}

The delegate type name can be custom or pre-defined. Net Bcl delegate type. We recommend that you use eventhandler, which was defined:

Public Delegate void eventhandler (Object sender, eventargs E );

The first parameter: the object sender is used to save the reference of the object that triggers the event. Because it is an object, it can match any type of instance.

The second parameter is used to save status information that is appropriate for the application.

However, the eventargs class is designed to not pass any parameters. It is used by event handlers that do not need to pass data-usually ignored.

If you want to pass data, you must declare a class inherited from the eventargs class and use appropriate fields to save the data to be passed. For example:

Public class myargs: eventargs <br/> {<br/> Public String message; <br/> Public myargs (string s) <br/> {message = s ;} <br/>}

Note: The class has nine types of members: method, attribute, event, constructor, finalizer method, operator, and index; data member: Field and constant.

Among them, the first three types are often seen or referenced by the outside world. Therefore, an event is a variable of the encapsulated delegate class.

 

 

Vi. interfaces (including some knowledge about enumeration and LINQ)

1. The interface indicates a group of function members without implementing the reference type of the members, including the declaration of the following function members: method, attribute, event, and index. Bcl has a built-in icomparable interface, as shown below:

Public interface icomparable

{Int compareto (Object OBJ );}

Do you know the static method: array. Sort (array) can sort numbers, English letters, or Chinese characters? It calls the compareto method implemented according to the icomparable interface in this array. If yes, it can be sorted. If not, there is no way to sort it.

Note: The interface is of the reference type. Therefore, you can forcibly convert the Class Object Reference to the interface type to obtain the reference pointing to the interface. Once an interface is referenced, you can use the dot to call the interface method. For example, IMy = (IMy) MC;

2. Why can foreach list all its contents? Why is it so amazing?

The reason is that an array can provide an object called enumerator as needed. For an object with an enumerator, you must have a method to obtain them. The standard method for obtaining an object enumeration number in. NET is to call the getenumerator method of the object. The type of the getenumerator method is enumerable ). An array is an enumerative type.

(1) ienumerator interface. It contains three function members: Current attribute, movenext, and reset method.

(2) ienumerable interface. Only one member: the getenumerator method.

(3) Generic ienumerable <t> interface. It also contains a method: getenumerator. However, in this version, the getenumeratorfanui implements the Class Object of the generic ienumerator <t> interface. Because the class must implement two getenumerator methods, we need to display the non-generic version, and implement the generic version in the class.

In short, because there are three members of the I. Or interface, foreach can list its content cyclically.

You can use the iterator to create enumeration types. For example:

Class myclass <br/>{< br/> Public ienumerator <string> getenumerator () <br/>{< br/> ienumerable <string> myenumerable = blackandwhite (); // obtain the number of enumerations. <br/> return myenumerable. getenumerator (); <br/>}< br/> Public ienumerable <string> blackandwhite () <br/>{< br/> yield return "black "; <br/> yield return "gray"; <br/> yield return "white "; <br/>}</P> <p> class Program <br/>{< br/> static void main () <br/>{< br/> myclass MC = new myclass (); <br/> foreach (string shade in MC) // use a Class Object <br/> console. write ("{0}", shade); </P> <p> foreach (string shade in MC. blackandwhite () // use the class enumeration number method <br/> console. write ("{0}", shade); <br/>}</P> <p> // The following output is generated: black gray white black gray white

 

3. LINQ

The query result of LINQ is actually ienumerable <>, for example:

Ienumerable <int> lownums = from N in numbers select N;

Two types of results can be returned for a LINQ query: ienumerable and scalar. For example:

Int numscounts = (from N in numbers select N). Count (); // returns an integer.

It is important to understand the content of the query variable. After the preceding code is executed, lownums does not contain the query result because it contains ienumerable <int> type objects (VaR can also be used for computer identification and replacement ), it is a reference type, and it is lownums in the stack. The reference result is the ienumerable <int> In the heap, which then points to other enumeration numbers in the heap. Therefore, the query expression returns an enumeration. The query will not be executed until the enumeration is processed. If an enumeration is processed multiple times, the query is executed multiple times.

If the query expression returns a scalar, the query will be executed immediately and the results will be saved in the query variable, that is, the stack.

 

4. C # events and flex events

In my opinion: BTN. Click + = btn_click in C;

Equivalent to BTN. addeventlistener (mouseevent. Click, "btn_click") in flex ");

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.