Dr. GUI .NET 1.1 # 0 - Introduces .NET, Hello World and substantially understand the internal mechanism of .NET runtime

xiaoxiao2021-03-06  62

Release Date: 6/15/2004 | Update Date: 6/15/2004

Revised for Microsoft .NET Framework version 1.1

Summary: After explaining why the .NET framework is fun, Dr. GUI introduces the .NET framework, Visual Basic .NET and C #. Then, the Ph.D. uses Visual Basic .NET, C # and ASP.NET demonstrates the "Hello World!" Program and describes how to write a switching function in the .NET framework. Finally, Dr. GUI demonstrates the IL and metadata of several programs, letting you see what is hidden under the appearance of the frame.

content

This page

Dr. Gui .net is back ... What is .NET? Why is the charm of the .NET framework to learn .NET framework? What will we discuss? Installation considerations for .NET programming with C # and Visual Basic .NET Write Hello World .NET to use ASP.NET swap variables to try! join us! Where to learn this theme and the next theme

View the source code of the list file provided in this article.

Dr. gui .net is coming back ...

DR. GUI's tutorial series may have discovered a good doctor for a long time, because he is recently vacation. Now he is back, Dr. Gui .Net column will be better than the past.

After various updates, Dr. GUI is currently revising articles, so that they meet the final release of Microsoft? Net Framework and Microsoft Visual Studio .NET 2003. Revised series uses C # and Microsoft? Visual Basic? .NET in the client application, and uses Visual Basic .NET in the Microsoft® ASP.NET application.

Therefore, a good doctor has accepted the same old mission, but now the mission has been updated. This is to help you understand the .NET framework and understand how to use it is most beneficial to you, no matter what kind of programming you are doing now.

Why should Dr. Gui .NET?

Before you continue to read the contents of the following, it is necessary to understand what this column will discuss, who may benefit from it and how to read this column.

Simply said: DR. Gui .NET introduces you how to program the .NET framework. Whether you choose to use C # or Visual Basic .NET, you will find an article for this language and code written in this language. (If you use Microsoft JScript? .NET or C , Dr. GUI, I have to apologize to you in advance. The time is limited, and it cannot be fully involved.)

So, if you are a language C, C , C # or Microsoft Visual J ? Programmer (or you are using the name "J" starting with "J", you will be "BRAND J"), then you will hope Use the C # to understand the .NET framework. Please continue reading. If you are a Visual Basic programmer, you can just get the content you need here.

Since several languages ​​are used, everyone will understand at a point in other languages. This is a good thing, and the debate about the language has never stopped, so many of the code will be written in several languages. (Dr. GUI speculation can get a salary in two languages ​​or multiple languages.)

But please wait, there are other things! In addition to the console application, we will also write all examples as an ASP.NET application. They are not highly level ASP.NET applications, and you don't have to learn how to be a good ASP.NET programmer (for such information, see http://asp.net/). But if you are an ASP or ASP.NET programmer, you will be able to see the richness of the .NET framework makes your programming work easier and efficient, and you will become a better programmer. If you are not an ASP.NET programmer, you will have the opportunity to learn about ASP.NET programming, which may inspire you to learn more in other places, and use ASP.NET to make some more advanced programming . In order to write this column, Dr. GUI did learn from ASP.NET programming, and did have fun, he hoped to have more understandings in the future, because for people currently do not have Web programming experience, ASP.NET is indeed Web programming is easy. ASP.NET makes web programming and fun because it is easy to use.

For the sake of simplicity, after this column, most ASP.NET applications we have written will use Visual Basic .NET. Dr. GUI once again apologize to people who like to use C #, but please note that the C # console example is converted to ASP.NET is very easy, especially because each time there is Visual Basic .NET console application and Visual Sample of Basic .NET and ASP.NET as a guide. (View the source code provided with the list of this article.)

Who should read Dr. Gui .NET?

Who should read Dr. Gui .NET? They are anyone who wants to program the .NET platform, including C #, C, C , Visual J , Brand J, and Visual Basic programmers, and people who have programmed in any language as ASP and ASP.NET. What will you understand? You will learn about the formation process of the .NET framework and its working principle, and learn how many of the contents of how to write a valid program (regardless of what kind of application written).

Although there is no discussion of everyone to discuss all aspects

Dr. GUI .NET cannot discuss all aspects for everyone. If you want to know some content, such as advanced ASP.NET application development and database access technology, this article will not fit you. Fortunately, there are a lot of good information sources, and one of the best entry information is http://asp.net/.

Why choose Dr. Gui .NET?

Those who have been reading "Asking Dr. Gui" and the older Dr. Gui .NET version knows Dr. GUI is very keen on Microsoft .NET, even if he can't always determine at any specific time. Net will include Everything is and what does not include anything. (If you want to find this, please read the official .NET Web site.) All name confusion makes Dr. Microsoft ActiveX? This is where there is marketing operation, where there is a name confusion. Dr. GUI likes "X" in "ActiveX" is assumed to be a silent this opinion, although this is not persistent. The same thing in the .NET world is this point (".") Whether there is a space. Rules (understanding at least Dr. GUI) is: products, such as Visual Studio .NET; but technology is not, such as ASP.NET. (Dr. GUI doesn't even want to know why.) No matter how much doctors are accurate, what is the part of the .NET, and how many changes in subsequent years, but he does understand the developer is very important Key section: .NET frame. He is not only "understanding" this, but he is very enthusiastic about this, it will make development work more interesting and more efficient. As the .NET framework will continue to improve in the next few years, soon, it will be something we are indispensable at any time.

Get .NET

Now you can get the .NET framework 1.1, whether there is any Visual Studio .NET. Also, if you are writing ASP.NET applications, you have now available for your trial for you to try your trial now, you have a new free IDE (called "Web Matrix").

.NET frame: Free!

The .NET frame itself (including compiler) is free. You can find a .NET Framework SDK and re-distribute file in .NET Framework Downloads. From this URL, you can also download the re-distributable part required to run the .NET Framework application you (and your customers). These two are free.

Web Matrix: Free!

The following will be discussed, Visual Studio is use the .NET framework is also a tool for writing a variety of applications using conventional native code in C / C .

However, if you only write ASP.NET applications, you may try to try Web Matrix. Web Matrix is ​​a Technical Preview (0.5 version) for building an IDE of the ASP.NET Web application, and this IDE is widely supported by professionals. It doesn't have IntelliSense and there is no debugger and does not have its own, with an indexed faster helper system, but depends on MSDN Online as a help system. So Visual Studio is still worthwhile. On the other hand, its download file is only 1.2 MB, and it is free, then why don't you try it? You can find it at http://asp.net/webmatrix.

Visual Studio: Not free, but worth value!

Although it can be free for the .NET framework development program, Dr. GUI warmly recommends that you purchase Visual Studio .Net Professional Edition (or higher), or to buy at least one single language standard version, such as Microsoft Visual C # ?. Net. Standard Edition suggestion retail price is $ 109, which is really cheap. If you are a MSDN subscriber, you should receive your Visual Studio .NET (if you haven't received it yet) soon. You can also download it (very large) in MSDN Subscriptions. (Subscribe to MSDN is a best way to get the remaining tools required for Visual Studio .NET and the development of Microsoft? Windows?) If you do not subscribe to MSDN Professional, Enterprise or Universal, you can purchase on your favorite software retailer. Visual Studio .NET or a single language standard version. For related matters, see the Visual Studio .NET site. The "developer Tools" link lists each single language standard version.

If you are used to using C / MFC like Dr. GUI, you will make such a combination of Visual Studio .NET and ASP.NET Web Forms to make it easy to surprise. In the past, since the debate, request / response programming model and the complication of HTML and script hybrid code were set, the DR. GUI has tried no longer writing web applications. However, Visual Studio .NET can automatically set applications in the web server, and the control / event model makes web forms as easy as the Write Visual Basic code (and does not need to write HTML unless you want to do this), Since the programming language for all .NET Framework applications is exactly the same language, you have a powerful standard language, which is an incredible rich library for you to use. In other words, Web programming is now interesting and easy as writing MFC or Visual Basic applications.

If you are an ASP programmer, you will be so simply surprised to write the easiness of writing web forms and WEB pages that you can work well on different types of browsers. Once the .NET framework is understood, you will thank the framework for the incredible powerful features you provide so easy to use.

But what if you use C / C to write the application of this code? Does Visual Studio .NET 2003 have any tools for you? Of course. Simply recompile the code, you can get a more compact, faster code. However, the C compiler complies with the extent to be much stronger than before, which allows you to be more easier to double-way with other compilers, and allow you to use the previously available libraries. For more information on this, see What's New In Visual C .NET 2003.

We will skip discussions on how to use Visual Studio and Web Matrix (just this column) so you can self-study Visual Studio and / or Web Matrix what can be done for you, but we will then use them because it is very easy.

Back to top

What is .NET?

We have just discussed that it is very difficult to say that .NET is or not every time point. But if you understand some of the key driving factors behind .NET, it will be easier to know what it is. How the world has changed in the past

Consider some trends in our industry. Many people are moving from the use of isolated PCs to PCs and a large stack of devices connected to multiple applications while connecting to multiple applications with Internet (wired and wireless, baseband, and broadband). They begin to expect to access important information in a way that can be used in any device that can be used. Since the information can be accessed via the Internet, they want to be able to combine raw data from multiple information sources into meaningful information. Finally, they expect to use software if they do not encounter in trouble, and expect that this will be more reliable than PC past practices.

And this is just a change in the client computer. The Internet has made the server-based application more popular than previous times and has made the connectionless request / response programming model more common. However, communication between servers (especially between servers running on different platforms) is very difficult, similarly, the difference between various browsers and all versions of the browser is also difficult, not to say all differences The type of wireless device is difficult. Since most actual Internet applications are centered on a database, it is more important than past data sources to easily access a wide variety of data sources.

How to help you.

The .NET framework helps to make these types of problems easier to resolve. For example, the .NET frame compression version (for equipment) is delivered with Visual Studio .NET 2003, so you can use the excellent public language running libraries in various devices (not just in the PC).

By adopting the Internet standard, .NET is designed from the beginning to work well on the Internet, including supporting XML web services, allowing programs to use XML to easily get data from multiple sites, and facilitate different Data exchange between computer systems. .NET also makes access data easier and makes it easier to write web applications that can run well on a wide variety of browsers and devices without having to override applications for each browser and device. Finally, in order to make it easy to use and improve reliability, the .NET is also supported simplified and reliable installation and upgrades. This level and .Net runtime security and version control feature improve the reliability and make the software can be sold as a service rather than the product.

Use the XML Web service to make the data set from multiple information sources into information available

Another important trend is to integrate information from many sources in order to do some interesting things. Typical examples are planned business travel: you need to buy a plane ticket, rent a car and stay in the hotel. You want to buy these three aspects to meet the services you need, so you need to get data from three different companies. And your travel also needs to be coordinated so that you can reach on time and have cars and hotel rooms when you arrive (of course, there is a broadband), and you can return in time. XML Web Services uses SOAP and standard XML architectures, which helps each application communicate through Internet standards so that applications can use other computers information and share information with other computers, regardless of other computers? . And the .NET framework is almost a very easy thing to write and use web services. However, if you want to see an example of using a .NET implementation, large and real web services, visit http://terraserver.microsoft.net/, this example provides maps and aerial photo information of the world ( Support for Microsoft TerraService projects, there are other information, such as US census data. Finally, Microsoft and other companies will provide interesting features as an XML web service, you can use these services like using local objects, of course, not there. If you get the needs you need to use the price, use OPWS (others' web services, this is the variant of the sec, and the SEC is the code of others) is always good.

So, the .NET framework will make it easier to write new types of applications, such applications are easier to install more and more reliable than before. Also, these applications will be powerful because they will be able to take advantage of XML Web services provided by Microsoft and others (maybe you)!

Back to top

The charm of Dr. GUI in the .NET framework.

Simple history of Microsoft operating system API collection

Using the .NET framework programming is different from the Microsoft Win32? API programming, as Windows programming is very different from DOS programming. Each of these APIs is its era product: The early 1980s is DOS, the mid-1980s is Windows and the late 1990s is .NET.

DOS

The DOS API is conceptually traced back to various single task operating systems designed for small computer systems. Although the small computer is popular when the microcomputer is first appearing, the microcomputer does not have sufficient memory or speed to run these relatively complex operating systems. Strict restrictions on memory and speed, such as DOS, such as DOS, mainly written in assembly language so that their speed can be as fast as possible and volume can be as small as possible. The DOS API is mainly a set of software interrupt calls (Remember INT 21H?), You absolutely need assembly language to perform calls (perhaps by using at least part of the runtime library written), the other DOS also provides a small amount (Dozens) services, but most of the disk and character mode I / O. DOS only supports a single task (so there is only one execution thread), and the DOS program has always controls the computer system. When it does not do other things, it is ready to detect the keyboard input by looping.

Windows

The Windows API is designed early in the 1980s. At that time, the root is the president, and C is the language of most system programmers selected. (Some important systems are also written in PASCAL, in terms of function, the language is very similar to C.) Stroustrup milestone book The C Programming Language is not released until 1986. "Structured Programming" is the stylish at that time, then the programmer learned to avoid GOTO, and object-oriented programming (OOP) has become mainstream until ten years. As a result, Win32 API has become a API that only C (or PASCAL) programmers can like. At this time, there is a namespace but only one, all hundreds (then thousands) API names are very long, and the API Author puts forward various naming conventions to classify them. The language does not directly support the abstract data type, so the programmer proposes various naming schemes, for example, Microsoft (proposed by Charles Simonyi) Hungarian program, which uses abbreviations representing the variable abbreviation as an identifier prefix (for example, "i "Represents index, or" c "represents counting, both of which use basic language type" int ").

However, the most important difference in Windows is that in order to utilize graphical user interfaces, they must be fully restructed. Because Windows supports multitasking (although multi-threads are not supported in Win16), it needs to return control to the operating system when your program does not do anything. Therefore, your program does not have always maintained control, but is re-constructed as a set of messages, each message handler quickly responds to messages from the operating system (eg, the mouse click, button or menu selection), and Return to the operating system as quickly as possible so that your app can wait for the next message. As an exchange of re-constructing your application, you can use rich Windows features, such as device independence, support for dynamic link libraries, and multitasking, and powerful standardized user interface features such as windows, dialogs, controls ( Such as, buttons, list boxes, and editing controls) and menus.

Object-Oriented Programming

Outside the 1990s, object-oriented programming became popular. Using object-oriented programming, it is possible to write larger items, such projects can define better interactions between program objects and less interaction. Objects allow programmers to create abstractions in a powerful manner, which makes programming to higher levels than previously.

Visual Basic? In its runtime environment, a lot of message transfer logic is encapsulated, and the programmer can make Windows programming in an object by allowing the Visual Basic programmers to focus on the object-based. As C becomes popular, class libraries (such as MFC) allow programmers to write their Windows applications as interactive object groups, which have a lot of default interactions provided by the application framework.

However, the API below the Visual Basic and the MFC program is still the old Windows API designed before the object-oriented programming. (Say fairly, it should be noted that Windows does reflect some object-oriented concept.) However, Visual Basic and MFC runtime library proves that this fact: it spent more and more effort to fill Object programming with old, monotonous API gaps.

Assembly

In the mid-1990s, new concepts are popular: software can be constructed with interchangeable components, which is like a single component to assemble an audio system or home theater. The idea is to implement the interface by careful defining the interface and implement them in a relatively easy way, and the software components can be mixed. Support for components-based programming requirements to add a lot of things to monotonous API (ie in Windows, Ole and CoM). Some of these adds is very complicated because C's-based API is always to support such operations such as dynamically create software objects and automatic objective life management, do not say that C and C expectations are to write an integral style. Applications, not applications consisting of a set of components.

As a result, writing components becomes bored, and requires files and converters to compensate for this fact: these languages ​​do not truly support component-based programming. Although Visual Basic and libraries such as ATL lighten certain monotonous, many advanced (but interesting) operations are still very difficult. Moreover, since Windows lacks support for flexible version control policies, new component versions usually cause existing applications to operate normally.

There is more functions but

Finally, this fact that the various APIs added to Windows year has produced huge inconsistency in the programming model. Even if the original Windows API also has some bad inconsistency - see how the windows, menus, and control methods are different from the device context, pen, fonts, and brushs, how different is different. But more serious problems is that there may be half-playing methods for errors in Windows. (Maybe more .dr. GUI has no listing.) String type may have a half-play. This is just a hoss of the iceberg.

Obviously, the timing of upgrading to new, clean design, consistent, and modern APIs are upgraded to new, clean design, consistent, and modern API timings. Microsoft's answer to these issues is the .NET framework.

Maintain a new beginning of coherence: .NET runtime

Just as the old DOS programming method was broken as the old DOS programming method (at that time, people often asked: What? Abandon TSR?), Now the Windows programmer starts with monotonous, old Windows API, turning to modern, facing Objects, when components-based API are time. Dr. GUI recommends that you use the .NET framework and run so that you can enjoy the benefits of modern running.

Thoroughly component-based components

To understand .Net runtime, you must understand this: .NET runtime is designed to provide best support for modern component programming, which is provided directly in runtime environments. In other words, it is completely targeted for components. If this is understood, it is easy to understand why .Net is designed to look like this.

As Windows adds direct support for windows, controls, drawings, and menus, and provides infrastructure, which provides an infrastructure based on messaging to support device independence, .NET runtime directly supports components (including attributes and events). Objects, inheritance, polymorphisms and interfaces.

Multi-language, interoperability with old code!

Also, whether it is running on your computer or on other computers, it provides this support while allowing you to use any of more than 20 different programming languages, and can still interact with non-.NET code. In other words, .NET represents a new, modern beginning, and maintains coherence: You can use a variety of languages ​​that are familiar with strangers and continue to use the old components and DLLs without modifying them. . You can even use the .NET component like any COM object, so your new .NET code can work with the old COM application. (You may want to compare the flexibility of this language and the interoperability of the old code with other runtime systems in the market, especially Brand J, and compared.) New Features Support Components

The .NET runtime also designed other interesting main features to provide the best support for component development. Direct support for attributes and events makes it easy to make components-based programming without need special interfaces and adapter design patterns. Automatic memory management allows you to assign objects and pass them between components, but don't worry that it should be responsible for the release of the object when it is no longer needed. Support for serialization can enable the "Freezing-Dry" component, including industry-based, XML-based SOAP (not just dedicated binary formats), including industry-based, XML-based SOAP (not just dedicated binary formats) and then reconstructed. Because you can call and create an industry-standard XML web service, you can use any of the people disclosed to any device on the Internet and use any of the components of any device from what is on the Internet. The abnormal handling mechanism provides robust, consistent error scenario. Each code module has built-in complete metadata, meaning that the function like dynamically created and method calls is easy and is a type of security. (Moreover, .NET framework even allows you to create and execute code instantly!) And you can control which version of the application uses components to make your application more reliable. Finally, since the code uses the intermediate language (IL) that is unrelated to the processor, not some specific machine language, this means that your component can not only run on a variety of computers, but you can affirm Your component does not overwrite the memory they do, it is impossible to cause malfunctions.

Each of these key functions is stored in .NET runtime to make a robust-oriented component makeup.

Waiting for one, etc.! And more!

In addition to all good component support features provided by .NET runtime, .NET framework also includes a huge amount, which is designed to provide an infrastructure to address current programming issues, including ASP.NET Complex, component-based web server applications support (and support for various browsers and mobile devices that act as clients), XML support, enhanced database access, and many other problems.

Part of the .NET framework will be used in the form of a .NET framework compression version, so learning the .NET framework for PC and servers will help you programmatically.

standardization……

Finally, as a main part of .NET runtime, the public language infrastructure (CLI) has been standardized by the European Computer Manufacturer Association (ECMA) and ISO (International Standardization) with the C # language and IL format, and ECMA is also completed. Standardization of JavaScript and JScript (http://www.ecma-international.org/publications/standards/ecma-262.htm). This allows other organizations to have the opportunity to complete with .NET compatible implementation on other platforms. For more information, see Ecma and ISO / IEC C # and Common Language Infrastructure Standards. It is also worth mentioning that Ximian, this is an open source software company, and a sponsored open source code is called Mono (http://www.go-mono.com/), which is committed to building a Open source code version of the .NET framework. If you are interested, you can find the interview between Dare Obasanjo's team leader Miguel de Icaza in Mono project.

Use the powerful Visual Studio .NET tool to program the .NET framework

The .NET framework is not only very exciting, but also when you use Visual Studio .NET to develop applications, you will also get a very powerful development environment, which allows you to easily provide you with all things. Documents are integrated into the Visual Studio .NET Integration Development Environment (IDE), including dynamic help, it can observe what you type and click content, and guess what help the topic is related to what you do. If the guess is correct (usually), click it, the help you need will display immediately.

Microsoft IntelliSense? Help you type correctly, variable name, and member name, and even display the parameter name of the method you call. (This feature can be used for the class you have written, and it is also used in the class in the library.) This makes the editing source code easier, faster, error chance.

There are still many other good features, in short, Visual Studio.net is really a very pleasant programming tool, and it is very efficient!

Back to top

Why learning the .NET framework?

If you have programmed another platform (such as Windows, Linux or Java), you may want to know why you need to learn this new platform: NET framework.

Dr. GUI looks away in this way: Since the .NET framework platform allows programmers to work very efficiently (in the mentioned platform, it may be the most efficient), so I don't know it is stupid. . It is not the best platform for all projects (there is no such platform), but our Ph.D. believes that you will find that it is the best platform for many projects. Because it can be well interoperability with other platforms, you may want some of the part of the project to complete in the .NET framework, and then for the rest of the project, can be used with this unit, COM object or Other computer systems perform interoperability.

For some kinds of applications, the .NET framework is a significant best choice. Using ASP.NET, web applications really easily write and powerful. With a .NET framework, XML web services are also very easy to write.

If you need to use the .NET framework in the project, you must learn it, please continue to read, Dr. Gui will help you. But if you are not sure, please continue to read. Just use a relatively short time, DR. GUI will help you understand the platform you like to use. (Even if you don't use it, understand it also helps you have other programming, just learning foreign languages ​​can help you better understand your mother tongue.) Dr.GUI's prescription: only one hour exercise .Net ...

... get high-strength programming skills

Because .NET is relatively new, Dr. GUI recommends that you should be in order to surpass the learning curve: Just find an hour to learn .NET every week.

These tutorial articles will be released twice a month, and they provide practicing opportunities to make you practiced what you are reading by completing these exercises. Therefore, assume that the article is read with an hour (Dr. GUI promises, the subsequent article will be shorter, and the reading time may be less) and do less to do it in one hour or two hours, you should use relatively little effort to learn some very Good new technologies, this, the average time is about one hour a week.

If you put in an hour a week, you will get a solid knowledge of the .NET framework. You will know how it work and can be programmed to .NET platform.

Study with a friend (or two, or more ...)

Just if there is a test partner to help you learn technology (this is your obligation), learn Dr. Gui .net every time you talk to friends or colleagues will help you learn better and have more fun. Alternatively, a better way is to organize a group of students together. Maybe you want to have a discussion group. You may also send an email with each other when you practice each group or difficulties.

Dr. GUI .NET Forum on GotdotNet

On the GotDotNET, a new forum is also set up in this column: http://gotdotnet.com/community/messageboard/Messageboard.aspx? Id = 46.

Good doctors can't answer your .NET framework programming questions on the forum, but he will answer questions about these columns.

Getting started with free information

Don't forget that you can get the .NET Framework SDK from MSDN, including the compiler you need, or even a debugger, which is free. This is a good way to start learning. Although Dr. GUI is very certain, you will soon hope to buy a copy of Visual Studio .NET to get a good IDE, help your system and debugger, not to say IntelliSense features.

Back to top

What will we discuss?

In the face of huge .NET framework, you may want to know what we will discuss and when?

We will start discussions in this column with a very basic knowledge. You have already read the .NET framework designed to solve something problem. Subsequently, we will discuss all aspects of .NET runtime, including an indispensable "Hello World" program, and introduce metadata and intermediate languages ​​(IL) (this is a .NET machine language).

Next time, we will start discussing the basics of the .NET framework, including all kinds of maternal system.object. We will also discuss garbage collection and a little knowledge about the interface.

After this, we will use several articles to introduce .NET framework and runtime foundation: data type, exception, file, and network I / O, serialization, thread processing, and timer, and debugging.

Then we will discuss the closest things with Windows programmers: graphical user interface class. (This may not be an Asp.net programmer is interested in content.)

The .NET Framework provides database access, XML and web applications offer a lot of good features, but our good doctors are planning to let others introduce these questions. Each column has an example program, an example of an explanation and an exercise you can try. These examples are written in C # and Visual Basic .NET (including ASP.NET versions with Visual Basic .NET), they focus on greater concepts so that you can better understand the design philosophy behind .NET.

Why use C # and Visual Basic .NET?

If .NET supports 20 types of languages, why is Dr. GUI's .NET column only use C # and Visual Basic .NET? At first, the test version of the DR. GUI .NET column only uses C #. When a doctor starts to do this column, C # becomes an inevitable choice. The reason is: Most of the .NET framework is written in C #, so most of the early examples also use C #. This seems to be an inevitable choice at that time. (Don't forget Dr. Gui is also a person who is familiar with C .)

But now, Visual Basic .NET is almost as strong as C # (they each have their own unique features), so it is the same good choice for .NET programming, now it is obvious, DR GUI .NET should use both languages ​​simultaneously. Thus, Ph.D. (with the help of Coding4fun Duncan Mackenzie, thanks Duncan!) Already provided a C # and Visual Basic .NET examples at the same time.

But please wait, there are other instructions! In order for these columns to be related to ASP and ASP.NET programmers, Dr. GUI also uses Visual Basic .NET including examples of using ASP.NET. From these columns, you can't learn how to become a good ASP.NET programmer (there are many better places, such as http://asp.net/), but you will learn how to be on your ASP The .NET program is fully and tippely using all the features of the .NET framework, allowing you to make better programmers in the case of better understanding. And, if you are not an ASP.NET programmer, you will initially recognize the ease of use and powerful features of ASP.NET.

Although you can use a lot of languages ​​in .NET, C # and Visual Basic .NET are designed for use .NET, and they are specifically designed to support components-based programming. They are all programmed, especially when using Visual Studio. They are easily used, but there is no annoying problem with Brand J. For example, you can use C # and Visual Basic .NET to actually write SWAP functions, we will then introduce this in this column. (BRAND J could not do this is one of Dr. GUI to complain about the language.)

Dr. GUI believes that C # and Visual Basic .NET will be the most popular language for .NET framework programming. Which one should I use? Any one is good. In addition, by using C # and Visual Basic .NET, you will have the opportunity to start learning these two languages ​​and .NET. If you choose to use another language, you don't hinder you by using these languages; your understanding of the .NET framework and runtime will apply to any language you are using.

If you like to use other languages, convert the code into your choice of language will be a relatively simple process. But Dr. Gui is only one person, he has no time to do. If you want to take this job, you can put your conversion code in the newsgroups that have been established to discuss these columns. (This will be detailed later.) Now I use Visual Basic, why do you use Visual Basic .NET without using C #?

Dr. GUI has heard that many Microsoft Visual Basic programmers are considering switching to other languages ​​other than Visual Basic .NET when migrating to .NET, such as C #. Good doctors want to ask them to reconsider before doing.

He heard the reason like this: "Since Visual Basic has changed a lot, I'd better learn a new, more powerful language."

If other languages ​​are indeed more powerful than Visual Basic .NET feature, this may be right. However, the relationship between Visual Basic .NET and .NET framework is not like Visual Basic and Windows. Typically, all languages ​​of .NET are in the same level of movement, and more or less equivalent. The difference is small.

Memolive, Visual Basic for Windows requires a heavy runtime library to interface with the Windows interface. As a result, Visual Basic programming is very different from a WINDOWS programming with C or C in many ways. Usually this is a good thing, because Windows is very complicated, and Visual Basic programming is much easier. The problem is that Visual Basic for Windows is limited by more or less, and the programmer can only do things that the runtime library designer wants to get in design running time. If you want to do what they don't plan, it will be very difficult, sometimes it is almost impossible. (Using a computer, nothing is impossible.)

On the other hand, Visual Basic .NET is just like each .NET language, directly with the .NET framework dialog. (There is a small Visual Basic .NET runtime, you can provide some of the Visual Basic features in the .NET framework. But it is completely different from the old runtime.)

In other words, Visual Basic .NET provides all the features of the base platform. Visual Basic .NET can almost anything C # can do. Now, they have some differences in language characteristics because each language has a small part of other languages. But these differences are very small.

For example, because Visual Basic .NET supports advanced binding, this feature is much easier than other languages ​​with Visual Basic .NET. Moreover, Visual Basic .NET can automatically link events with the correct named event processing method, saving some of the code necessary in other languages.

On the other hand, C # allows for writing "unsafe" managed code that can be manipulated with a way similar to C and C (more accurately, this is the code that cannot be verified as type secure). And C , which use C hosted extended C allows to perform operations such as mixed hosted and unmanaged code. But you need a very specific reason you will do this, but will not be a daily programming need.

But you don't have to pick a universal language: Use the .NET framework to provide very well-mixed language features, you can write only the program part of these languages ​​with C or C #, and then do the rest with the language you selected.

The key here is that if you are already a Visual Basic programmer, it is not necessary to use a new language to use the .NET framework. You can learn the difference between Visual Basic 6.0 and Visual Basic .NET, focus on the .NET framework, including inheritance and exception. You will be able to do all things that C # programmers can do. Also, if you decide to learn C #, you will use Visual Basic .NET to transfer almost anything to C #. (Dr. GUI speculation) The programmer who knows Visual Basic .NET and C # will have higher salary than those who only know one of the languages, so as in real life, bilingual is the advantage.) So, DR. GUI's suggestion is:

• If you primarily use Visual Basic programming now, learn the Visual Basic .NET and .NET framework. If you need, you can then learn C # when you are free. Almost everything in Visual Basic .NET is suitable for C #. • If you are programmed by C, C or Brand J, learn the C # and .NET framework. (You can continue to use C or J #, if you like.) If you want to increase salary, then learn other languages, such as Visual Basic .NET. • If you need to interoperate with many unmanaged C / C code, or need to utilize every performance, you do need a quick run code using C managed extensions, and use C # for the rest of the code. • If you primarily use Microsoft Jscript? Or JavaScript programming, learn the JScript .NET and .NET framework, if you want to extend to other languages. • If you are in the management project, select the language that is most in line with your team. And freely use another language to resolve the part of the problem that the language can be solved.

In other words, what language is really indispensable. In the .NET framework, you can use any language to do anything. Of course, different language features will make some tasks more easier than other languages ​​than other languages. For example, in the previous mention, Visual Basic .NET provides automatic advanced binding, but if you use C #, you need to write a few lines of code to cause advanced binding. That's why you can easily mix the cause of the various languages!

Therefore, use your favorite language at first, then use other languages ​​when you have time and willing.

Or, if you like, learn new languages. This is completely determined by you. Just know, you don't have to learn new languages ​​in order to use all the features of the .NET framework.

Back to top

Installation Precautions

Most installations are very simple, just click the appropriate installer or URL, then you can complete the operation. The installer is both easy to understand. (If you want to use ASP.NET, be sure to enable Microsoft® Internet Information Services before installation [IIS].

There is a small skill, although: When you are installing the .NET Framework SDK, the system may ask if you want to register environment variables to compile from the command line. You want to do this so that you can use tools from the command line. Make sure the check box is checked to register the environment variable and can use the command line tool.

If you install Visual Studio .NET, this check box will not be selected. You should start the command prompt using the Visual Studio .NET 2003 Command Prompt shortcut, which is located on the Start menu below Microsoft Visual Studio .NET 2003 / Visual Studio .Net Tools. Use this shortcut to enter the command prompt environment to set the correct path. In addition, during installation, please make sure that any programs are started, such as Microsoft® Internet Explorer or Microsoft Outlook?. Dr. GUI did not do this, and the result had to be repaired. Please wait until Visual Studio .NET 2003 installation is complete, then receive email.

Back to top

Programming for .NET

Visual Studio .NET is designed to make .NET programming than other methods, such as automatic statements complete, and drag and drop form editing such, making it easier to use unfamiliar API collections. It will help the system to integrate into IDE, which does have brought a huge benefit.

In the future, we will use Visual Studio .NET, but in this column, our Ph.D. still hopes that you can use the old way, even if you already have Visual Studio .NET: use Notepad (or other editor, such as Visual Studio If you like) write a source file and save it to disk, then compile and run from the command line. (For ASP.NET applications, copy the code into the appropriate web application directory and use Internet Explorer to view this page.) Do just .NET SDK without the need for Visual Studio .NET. Use the command line tool to make us have the opportunity to see what files have been generated and see what happened in the background.

How to spell?

In the .NET program, uppercase and lowercase usage agreements may differ from other languages ​​you are using. However, .NET rules are simple: in addition to the parameter name and private field name, all the first letters of each word of all identifiers should be capitalized, including the first word. This rule is called "Pascal Size", from the public agreement between Pascal programmers.

The first letter of each word of the parameter name and the name of the private field is capitalized, but the first word except. (Microsoft recommends that all fields are private; however, you can use protected or public attributes to disclose them.) This is called "Camel Size Rules". Dr. GUI is not very certain why this is called.

You really need to know these rules, because .NET Framework names comply with these rules, and some languages ​​(such as C #) are case sensitive.

Therefore, the name of the main output function is System.Console.Writeline, which is fully compliant with the first letter of uppercase rules. The name of the format string parameter of System.Console.writeline may be formatstring instead of formatstring. However, the name of the string length attribute may be Strlen instead of Strlen.

For Dr. Gui, the hardest thing is to remember to use main instead of main.

Back to top

Write Hello World .NET with C # and Visual Basic .NET

It is no longer, the following is the simplest "Hello World" written in C # for .NET:

C #

// compile with: csc hellocs.csclass myapp {

Public static void main () {

System.console.writeline ("Hello, World! (From C #)");

}

}

Below is the code implementation in Visual Basic .NET:

Visual Basic .NET

'Compile with: VBC Hellovb.vb

Class myapp

Public Shared Sub Main ()

System.Console.writeline ("Hello, World!" _

"" "") ")")

End Sub

END CLASS

Run the program

To run these programs, simply create (or obtained from newsgroups) containing these code text files, and then compile the file using the command line displayed in the code. (If you need to set the path to work properly, please in the frame SDK / bin directory [on the machine of Dr. GUI is: C: / Program Files / Microsoft Visual Studio .NET 2003 / SDK / V1.1 / bin] Run SDKVARS.BAT.)

Then, type the name of the program to run the program. For example, create a file, you can type the following program name:

REM C #

CSC Hellocs.cs

Hellocs

REM VB .NET

VBC Hellovb.vb

Hellovb

Everything is in the class

The first thing you are likely to notice is that the main function is a member of a class.

In .NET, all methods must be a member of the class or structure. (Next, we will discuss the structure in detail.) This is because the basic organizational unit in .NET is type (type, in addition to other concepts, also means or structure). Visual C .NET managed C extension allows the number of variables and functions existing on the surface on the surface, but even these variables and functions exist in the following classes: an implicit global class.

have a look! No header!

Not only all content is in the class, but also the class declaration is also complete. In addition to the source file, there is no header file, IDL file, or any other file. All things needed when describing classes are together.

So, what happens if you use another class in the source file? How do the compiler know what this class is?

Answer Yes, the compiler reads the metadata of the assembly, and the assembly contains the class you are using. The program just shown is implicitly using System.Object (because all classes are derived from System.Object). The compiler is automatically transferred to the MscorLib.dll assembly to obtain metadata of the assembly, so it can get the type information suitable for system.object. If you use a class from different program sets, you need to use the / R option on the compiler command line to specify the assembly, such as:

REM C #

CSC /R: FOO.dll Hellocs.cs

REM VB .NET

VBC /R: FOO.DLL Hellovb.vb

By the way, Module is different (different from the Module concept in Visual Basic .NET!) Is an executable: can be an application or a library. Its extension is usually .exe or .dll. Assembly is a collection of one or more modules. A file in the assembly contains a list of assemblies, and the list is a list of files included in the program set. Assembly is the smallest unit of executable code, and Assembly can be deployed and versioned. (You will notice, then, when we view the list of Assembly, only the ASSEMBLY has a version number.) Special main

Second, you are likely to notice the modifier of Main: public and static (Visual Basic .NET is Shared). In these languages, members' default access control is private; thus we need to declare that Main public can call it when it is run.

We declare the main declare to Static (Shared in Visual Basic .NET), which means it is a class method, not an instance method. This means that it can call it when running, instead of creating a MyApp object and will pass the THIS reference to the object (in Visual Basic .NET is ME). (Note that if we use Module in Visual Basic .NET, the method will default to public and shared.)

Note that the class can be named any name; here, I happen to name myApp. This version of the MAIN has no parameters and has no return value, which is represented by the return type of the VOID. In Visual Basic .NET, Main is a SUB instead of a function of return a value of VOID.

MAIN can be written to carry a String array containing the command line parameters, and / or returns an integer success code.

Finally, we call the WriteLine method to write the string. I have selected the fully qualified name using the function system.console.writeLine. In the System namespace, WriteLine is a Static method for the Console class (like main).

Save some typing content

Type a fully qualified class name is very troublesome, so C # and Visual Basic .NET allows the use of the USING (or Imports) statement to specify the namespace to use. E.g:

C #

// compile with: csc hellocs2.cs

Using system;

Class myapp {

Public static void main () {

Console.writeline ("Hello, World! (2) (from C #)");

}

}

Visual Basic .NET

'Compile with: VBC Hellovb2.vb

Imports system

Module MyApp 'VB-ONLY CHANGE

Sub main ()

Console.writeline ("Hello World! (2) (from VB .NET)")

End Sub

End module

Because the compiler searches for the system namespace and local namespace, you do not have to specify System when calling console.writeLine. This is the benefits here, but if there are many object references and method calls, it will be very convenient, you will see this in the later program. Note that the namespace (System) and the assembly you will find in it will be two complete things. A given assembly can contain classes from multiple namespaces; Similarly, classes in a given namespace can be dispersed into multiple assemblies. There is no relationship between a class logic that belongs to a part of the namespace that can be found in it in which the binary code of the class is found. Use the / R option to tell the compiler to find the binary code of the class. Use the uses and imports statements to tell the compiler to use what namespaces are used.

Only Visual Basic: Module and class

In the Visual Basic .NET version we conducted another change: we did not let MyApp become a regular class, but make it a special type of class called Module. Module is the role played by C # medium-static classes (if C # has such a role). In general, all methods are shared (static in C #) and public; therefore, we don't have to declare Main as Shared or public. Note, you can't create an instance of Module like a class.

Waiting first! Don't leave first!

In some cases (for example, if you run the Hello program from the File Explorer), the command window may disappear before you see the output. To avoid this, read the input from the keyboard by calling the console's Readline method (this will cause the program to pause until you press Enter). (For console applications, we will always call this as the last statement in Main.) This, our program last looks like this (we have returned to the class in Visual Basic .NET):

C #

// compile with: csc hellocspause.cs

Using system;

Class myapp {

Public static void main () {

Console.writeline ("Hello, World! (PAUSE) (from C #)");

Console.readLine (); // Wait Until Enter; Last Line

}

}

Visual Basic .NET

'Compile with: VBC Hellovbpause.vb

Imports system

Public class myapp

Public Shared Sub Main ()

Console.Writeline ("Hello World! (PAUSE) (" from vb .NET) ")

Console.readline () 'Wait Until Enter; Last Line

End Sub

END CLASS

Hello World in ASP.NET (with Visual Basic .NET and C #)

About web applications

When writing ASP.NET programs, you have written a program that will create a web page and interact with it, so your program is not as simple as writing a few characters to the console and wait for input as simple. Your program will communicate with users using the web page and need to respond to the user on the web page. This additional complexity must also be taken into account even if it is a simple matter like "Hello, World!". The purpose of the earliest, the purpose of the web is to distribute documents, so the web server will only get URLs from the browser and pass the disk file corresponding to the URL (formatted to HTML). However, only the static content is transferred, and very complex applications are not allowed, the web server quickly evolves to support forms and various types of dynamic content. One common technology to perform this task is to send data in the form to a separate program, and then analyze the fields in the program, and determine the operation to be taken, and finally generate the response to display in the user browser.

ASP

A more popular way to link the web page with the code running on the server is Microsoft Active Server Pages, which is more commonly used as ASP. In ASP, the interpretative script (usually Microsoft Visual Basic Scripting Edition (VBScript) is sometimes established within the Web page in JScript or several other languages). When the server processes the page, it explains and executes the script while reading the entire page to determine what response is sent to the browser. This html and programming integration makes it easy to write ASP pages and is therefore very popular. But it means that the programming logic (script) is bundled with the same file containing the format (HTML).

Where is the script?

Note that the client script (here, the script is sent to the browser as part of the HTML page, the browser executes code, usually using JScript / JavaScript) and server-side scripts (as just described, the script is on the ASP page In the server, VBScript is usually used.

Dr. GUI .NET series only discusses server-side scripts (at least most of these contents), as for client scripts, at least hundreds (if not thousands) can learn it.

Request / Response Model

Memolive, in the web page, you send some HTTP request (such as the data in the URL and form) to the server, and the server returns a response (web page). The browser will then display the response. When you click on a variety of web page elements (such as, buttons or links), one of the following two things will happen: If the web page has a client script code (downloaded with the web page, and usually use JScript / JavaScript) , The event handler may handle the event without even talking to the server. Or, depending on the HTML code of the web page element (such as button), your browser may send an HTTP request to the server. This is what usually happens when you click the Submit button on the form.

This programming model may be quite difficult to use because you have to know more information about HTTP requests and responses. And the model is simply different from most Windows applications (especially Visual Basic applications), simple event-driven forms and control models.

The programming model of the "Classic" ASP is based on the HTTP request / response model. It is basically a thin package that surrounds the request and response, obtains the requested field through the included infrastructure, and writes scripts to manipulate and query data. , Write the output in response. This is useful, but we can do better.

If you are used to the model, please don't despair: You can still run the ASP application with the ASP.NET application. If you know what to do with the application status and other questions, you can even convert the ASP application to ASP.NET.

Model based on ASP.NET Form

The programming model in ASP.NET adds a fundamental element: form and control. The programming model embodied by the ASP.NET web form is similar to the programming model of the Windows application (especially similar to the Visual Basic Form), although the mechanism below the programming model is very different.

From a conceptual view, the pure web form application consists of a group containing controls and HTML forms. The control has attributes and methods, and generates an event. These events are typically processed on the server, although certain events can be written on the client on the client. (But this is beyond the scope of this column.)

In other words, the Web Form Programming Model is actually the same as the Visual Basic programming model. So if you know Visual Basic, you will like a web form. This is a simple model than the ASP model.

So, even the easiest application we have written here will be a completely modern web form application. Note, very interesting is that the language used to describe the web form layout is very like standard HTML (mainly in several properties and control names).

Now come to see the code ...

No longer continue, let's look at our first web form application, this time uses Visual Basic .NET, this example uses embedded code, ASP style, and is in the HelloinLinevb.aspx file, then C # Equivalent code:

ASP.NET using Visual Basic .NET