. NET Framework (1)

Source: Internet
Author: User
Tags rounds
I. Basic Concepts
The. NET Framework is an environment for developing, deploying, and running. NET applications, including ASP. NET, CLR, And. NET Framework classes. There have been many introductions about ASP. NET and CLR, but the introduction of. NET Framework classes is relatively rare.

The. NET Framework class, that is, the system class, provides a large number of core functions that can be used to construct ASP. NET Applications (and non-ASP. NET applications. The system class can be used in all. NET languages, so we can regard the system class as a. Net Windows API. Unlike windows APIs, the system class provides advanced interfaces similar to com, which are quite easy to use.

As for all other. Net classes, the system class also uses Program The form of an Assembly exists .. The Assembly in net is similar to the com DLL or EXE file-it is a save class Code . For example, the math class (including its attributes and method definitions) is located in the mscorlib. dll assembly .. Net has two types of Assembly: private assembly and shared assembly. Private assembly is an assembly of a single application, usually located in the bin directory of the application; in contrast, shared assembly can be used for multiple applications, and it should be loaded to the global assembly Buffer by the Assembly creator (equivalent.. net ).. . Net System Classes all belong to the shared assembly.

If you have used Windows API programming, you must know that the difficulty in using Windows API is: it is difficult for us to identify and find out which function to call. In Windows api dll, there is no way to organize functions. It looks like all API calls are randomly stacked into a large DLL. Fortunately, the. NET class is organized into a level called namespace according to the logical relationship. For example, the math class is a member of the system namespace. Namespaces can be nested in multiple layers. For example, the adoconnection class is a member of the system. Data. Ado class.

1.1 reference a member in a namespace
To use a class in a namespace, you must follow the namespace hierarchy to find the specific class to use, that is, explicitly reference the class by name separated. For example, to create an adoconnection object, we must use code similar to the following (all examples in this article are written in Visual Basic, But no matter which one. net Language, the basic concepts are still the same ):


Dim CNX as system. Data. SQL. sqlconnection
CNX = new system. Data. SQL. sqlconnection (_
"Server = (local); uid = sa; Pwd =; database = pubs ")




In addition to the above method, we can also use the import command to simplify the reference to the class. For example, the following code tells ASP. NET to import the system. Data. SQL namespace to the current page:


<% @ Import namespace = "system. Data. SQL" %>




After importing a namespace, we can directly reference the class name, omitting the namespace description before all class names. The code for creating an adoconnection object is:


Dim CNX as sqlconnection
CNX = new sqlconnection ("Server = (local); uid = sa; Pwd =; database = pubs ")




It can be seen that using the import command can reduce a large amount of input.

Many namespaces have been automatically imported by ASP. NET, so we do not have to import these namespaces to simplify name reference. These namespaces are as follows:


System
System. Collections
System. Text
System. Text. regularexpressions
System. Web
System. Web. caching
System. Web. sessionstate
System. Web. Security
System. Web. UI
System. Web. UI. webcontrols
System. Web. UI. htmlcontrols
It should be noted that importing a namespace with a higher level does not mean that the namespace under the hierarchy is imported at the same time. That is to say, the following import command only imports system. the class in the Data namespace, but not from the system. data. ado, system. data. SQL and other system. the namespace import class under data:


<% @ Import namespace = "system. Data" %>




1.2 class members
The class contains various Members, including properties, methods, fields, events, and constructor ). Attribute describes the features of a class. For example, the system. array class has a Length attribute. Methods are actions that can be executed by the class. For example, we can call the sort method of the array class to sort the array. The domain is similar to an attribute. For any specific application, we can treat the domain like an attribute. For example, the PI attribute of the math class returns the π value. An event represents an action that we can respond to. For example, an event named infomessage in the adoconnection class is triggered when the database provider sends a warning or prompt message. Finally, the constructor is a special method that is called when a new object is created. For example, when we create a new sqlconnection object, we call the constructor of this class and pass a database connection string to it.

A class member can be a static member or an instance Member. Static members (also known as shared members) are shared members of all classes and do not depend on specific instances of the class. To use a static member, we only need to reference the member name through the class name like an object. For example, the POW method of the system. Math class is a static member used to calculate the multiplication of values. The following code calculates the 3rd power of 5:


Answer = math. Pow (5, 3)




Unlike static members, instance members depend on specific instance objects. It means that before using instance members of the class, we must first create an object instance. For example, we can use the nextdouble method of the Instance member of the system. Random class to return a random number. The following code first creates a system. Random type object, and then sets the value of the dblrandom variable to a random number:


Dim dblrandom as double
Dim RND as random = new random
Dblrandom = RND. nextdouble




1.3 VB. NET functions and system class members
The history of VB. NET is a long and ever-changing process. Although Microsoft has removed many functions of the original VB in VB. NET, many functions of VB. NET are still the same as those of the system class. If you can choose, using the system class is almost always better than using the VB. NET function. Using the system class not only makes the code easier to be transplanted to other languages, but also makes the code more in line with future VB. net version requirements, because in the future VB. in the. NET language, Microsoft may continue to cancel some of the functions of earlier VB languages.

Ii. Mathematical computing
The system. Math class contains a large number of domains and methods that can be used for mathematical computation. All its members are static. The POW method of the system. Math class can calculate the multiplication of values. For example, we can use the math. Pi domain and the POW method to calculate the area of the circle (in the following example, dblradius is the radius of the circle). Math. Pi returns the circumference rate π:


Dblarea = math. Pi * Math. Pow (dblradius, 2)




The SQRT method can be used to calculate the square root of a number. For example, the following code calculates the square root of 64:


Answer = math. SQRT (64)




The ABS method can return the absolute value of a value. For example, the following code returns the absolute value of-7.8, and the value of answer is 7.8:


Answer = math. Abs (-7.8)




The sign method returns the numeric symbol. If the value is negative, sign returns-1; if it is positive, sign returns 1; if it is 0, sign returns 0. The round method rounds a value to the nearest integer. For example, if the following code rounds the value 3.4677789, the answer value is 3:


Answer = math. Round (3.4677789)




If you want to round a value that exactly falls between two integers, such as 3.5, the round always returns an even number closest to the value. That is to say, math. Round (3.5) returns 4, while math. Round (6.5) returns 6. The floor method truncates a real number. Its return value is the maximum integer less than the specified value. For example, the following code truncates a value of 5.9 and the answer value is 5:


Answer = math. Floor (5.9)




Note that the result of the negative number operation in the floor method may be different from what you think. For example, the return value of floor (-5.9) is-6.

In addition to the methods described above, the math class also contains many triangle and logarithm calculation methods.

3. generate random numbers
The system. Random class is used to generate random numbers. However, unlike the RND function of VB. random can return both fractional random numbers and integer random numbers. system. the random class automatically generates random number seeds based on the system date and time to initialize the random number generator.

The nextdouble method of system. Random can return a random number of the double type between 0 and 1. The next method can return a random integer between two integers. Nextdouble and next are both instance methods. Therefore, you must create a system. Random object before using these methods. The following is a complete ASP. NET page, which shows how to use these methods to generate 20 random numbers, 10 of which are between 0 and 1, and the other 10 are between 1 and 50:


<% @ Page Language = "VB" Explicit = "true" %>
<Head>
<Title> random instance </title>
<Script language = "VB" runat = "server">
Sub page_load (SRC as object, e as eventargs)
'Equivalent to the. NET Framework class of VB6 RND Functions
'Random Number Generation Method
Dim RND as system. Random = new system. Random
Dim I as integer

Lbloutput. Text & = "<Table border =" "1" ">"
Lbloutput. Text & = "<tr> <TH> RND. nextdouble </Th> "&_
<TH> next (1, 50) </Th> </tr>"
For I = 1 to 10
Lbloutput. Text & = "<tr>"
Lbloutput. Text & = "<TD>" & RND. nextdouble & "</TD>"
Lbloutput. Text & = "<TD>" & RND. Next (1, 50) & "</TD>"
Lbloutput. Text & = "</tr>"
Next
Lbloutput. Text & = "</table>"
End sub
</SCRIPT>
</Head>
<Body>
<Asp: Label id = "lbloutput" runat = "server"/>
</Body>
</Html>




The running result of this page is displayed (of course, if you run this page, you may get a set of different random numbers ).



We have discussed how to combine the Assembly, namespace, and class in. NET Framework into a whole, and how to use the built-in system class for mathematical computation and generate random numbers. The second part of this article will discuss several other useful classes, including the array, string, and datetime classes.

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.