F #: Leverage the. NET Framework

Source: Internet
Author: User
Tags datetime diff sleep thread tostring

F # acts as a language running on the. NET Framework, and it is natural to use the. NET Framework.

This time we'll try to write code by calling the. NET framework.

The code that ignores the return value first is written as follows:

Ignore (System.Windows.Forms.MessageBox.Show ("Hello World")

This will show the MessageBox. In addition, if you want to omit the namespace, you can rewrite it as:

open System.Windows.Forms
ignore (MessageBox.Show("Hello world"))

Of these, the using used in C # becomes open.

The code to use the thread is as follows:

open System.Threading
Thread.Sleep(4000)

Next try using datetime and Console.WriteLine:

open System
let dt = new DateTime(1984,1,24)
Console.WriteLine("{0} {1} {2}",dt.Year, dt.Month, dt.Day)
let today = DateTime.Today 
Console.WriteLine("{0} {1} {2}",today.Year, today.Month, today.Day)
let diff = today - dt;
Console.WriteLine("{0}", diff.TotalDays)

Also, the variables for F # functions do not need ():

Thread.Sleep 4000

It is also possible to pass the pipeline:

4000 |> Thread.Sleep

What happens when you have multiple parameters? Try the following code:

Console.WriteLine "{0}" dt.Year
Console.WriteLine "{0}", dt.Year

Not at all. It is only when you feel that you have one parameter. The code is as follows:

Console.WriteLine (dt. ToString ())

However, the following code will cause a compilation error:

Console.WriteLine (123.ToString ())

The feeling is that numeric constants cannot use method.

Try writing some other code.

open System 
open System.Collections.Generic
let n = 123 
Console.WriteLine("{0}",n.ToString())
Console.WriteLine("{0}",Int32.MaxValue)
let rnd = new Random()
let list = new List<int>()
for x in 1..10 do
         list.Add(rnd.Next(1,10))
for x in list do
         printfn "%A" x

If you have used C #, you can use the. NET framework quite naturally. F # is a worthy. NET language Ah!

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.