1.5.2.2 to the end of robust

Source: Internet
Author: User

1.5.2.2 to the end of robust

 

Unlike many other simple and popular languages, F # is also reflected in other aspects, such as being able to write mature, robust, and secure code. The general process is to start with a simple code and refactor the code library to make it easier for other F # developers to access and write better documents. net and C # For better interoperability.

Encapsulating a function (encapsulate) into a. Net class may be the most important step in the net language. The F # language supports the complete. Net object model. Creating the F # class looks like a common. Net class and has all the ancillary contents of the class, such as static type information and XML documents.

In chapter 9, we will learn more about the F # object type. In chapter 4, we will see a lot of techniques for robust code. Now we can prove that we can use F # in the traditional. Net style #. In listing 1.12, we wrap the sayhello function into a C # style class and add a Windows Forms user interface.

 

Listing 1.12 object-oriented "Hello World" using Windows Forms (F #)

 

Open System. Drawing | [1]

Opensystem. Windows. Forms |

 

Type hellowindow () = [2]

Let FRM = new form (width = 400, Height = 140)

Let fnt = new font ("Times New Roman", 28366f)

Let LBL = new label (dock = dockstyle. Fill, font = fnt,

Textalign = contentalignment. middlecenter)

Do frm. Controls. Add (LBL)

Member X. sayhello (name) = [3]

Let MSG = "hello" + name + "! "

LBL. Text <-MSG <-- modify attributes of the. NET type

Member X. Run () = [4]

Application. Run (FRM)

 

Listing 1.12 starts with several open commands [1] and imports the types in the. NET namespace. Next, declare the hellowindow class [2]. The Code encapsulated by hellowindow constructs the user interface and exposes two methods. The first method [3] encapsulates the function, this is the "Hello World" message we used for Interactive Development earlier. The second method uses the form as a standard Windows Forms Application to run [4]. The declaration of this class is like that of a common C # class. The difference is that F # writes a class with a more lightweight syntax, the code for using classes in F # looks like the common C # code:

 

Let Hello = new hellowindow ()

Hello. sayhello ("dearreader ")

Hello. Run ()

 

At this stage, we develop applications in the traditional. Net style. Therefore, the program runs as an independent application. The interactive style helps us with the fact that we have tested the part for processing string connections. Result 1.4 of the application running is displayed.

Figure 1.4 run the winforms application created in the object-oriented style in F #

 

In this section, we have already experienced the typical F # development process, but we have not explained all the F # structures used, as we will discuss them later in this book. Although the example is simple, it shows that the F # language can write a fairly standard. Net program.

 

F # What can C # developers provide?

 

F # is very suitable for the simple concept at the beginning, and later converts it into a traditional. Net version, while C # is mainly oriented to the traditional. Net style. If you are a C # developer, you can easily use F # in two ways when writing a utility #:

The first option is to use F # for rapid prototyping, code testing, and how the. NET library works. As you can see, the F # interaction method is very convenient, so you can use F # to write the first draft of the code. When you try several ways to solve the problem, or it will save a lot of time to explore new library functions. If you must use C # To write code, and then use C # to rewrite the prototype, you can still save a lot of development time.

The second option is to reference the database written by F # in the C # project. F # is a fully compiled. NET language. Therefore, technically speaking, there is no need to prefer C # source code. In this way, you only need to simply convert the code into a traditional one. net version, you can easily access library functions from C #; for large.. Net solution. You can use F # To write a part of the solution.

 

This chapter is about to end [How sad]. It is very likely that you will still find some F # language structures confusing, but the purpose of this article is not to teach you all about F, our goal is to show the appearance and style of F # so that you can experiment with us as we explore more interesting examples in the following chapters.

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.