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!