【发布时间】:2018-08-31 09:52:52
【问题描述】:
例如定义一个打印“hello”的脚本。
我可以用 nodejs (hello_node) 做以下事情
#!/bin/node
console.log(`hello ${process.argv[2]}`)
那我就叫它
./hello_node "world"
在 Python 中也是如此
#!/bin/python3.7
print("hello world")
在 PHP 和 ruby 中同样有效,有没有办法在 C# 中做到这一点?
我尝试了以下方法,但没有成功
#!/usr/local/share/dotnet/dotnet run
using System;
public static class Program {
public static void Main(string[] args)
{
Console.WriteLine("Hello " + args[1]);
}
}
【问题讨论】:
-
什么“没用”?假设代码被正确嵌入到项目中,这应该工作。请记住,arrays 从零开始,所以如果你执行
dotnet run World,你会得到一个IndexOutOfRangeException,如果你执行dotnet run World Something,你应该得到Hello Something(忽略第零个参数World)。 -- 并且很容易创建一个项目,如下所述:.NET Tutorial - Hello World in 10 minutes