我已经整理了一个基本的命令行应用程序,以便更好地理解.NET运行时的工作原理。特别是,我想知道如果后续的分支语句使得它不需要程序的输出,那么在分支语句之前声明的某些代码是否会实际运行?
我的F#代码用于下面的简单控制台应用程序(带注释):
[<EntryPoint>]
let main argv =
// I have given the input "2" at the command line
let (status, result) = System.Int32.TryParse argv.[0]
let a = 1
let b = 2000 * 5 + 9 // Will this ever be evaluated at runtime ??
let fa x = x + 1
let fb x = x + 200 // In a similar vein, will this need to be declared at RUNTIME
let answer =
if result > 1 then
fa a // the path it will take at RUNTIME
else
fb b // the path it will NOT take at RUNTIME
printfn "%d" answer
System.Console.ReadLine() |> ignore
0 // return an integer exit code
总结:b
和fb
肯定会被编译成可执行文件但是它们实际上是为了运行程序的目的而进行评估还是运行时优化它们在运行时离开 ?