C#控制臺程序一個入門用的hello world程序如下: using System; class CSharpTest { static void Main() { Console.WriteLine("Hello world not using Qt"); } } 編譯 E:\Test1> csc hello.cs 運行 E:\Test1> hello Hello world not using Qt 有沒有問題?
入口點可用的入口點(需要時某個類的靜態(tài)成員函數(shù),MSDN中 Hello World in Visual C#中提到必須是public,但似乎private也沒有問題):
如果下面這樣的程序,還能直接csc hello.cs編譯么? using System; class CSharpTest { static void Main() { Console.WriteLine("Hello world not using Qt"); } } class CSharpTest2 { static void Main(string [] args) { Console.WriteLine("Hello world not using Qt too"); } } 如何解決?可以用 csc /main:CSharpTest hello.cs 或 csc /main:CSharpTest2 hello.cs 選擇入口點。 如果一個類內(nèi)多個入口點函數(shù),似乎就沒辦法了(fixme) 返回值
using System; class CSharpTest { static int Main() { Console.WriteLine("Hello world not using Qt"); return 1; } } 結(jié)果 E:\Test1>hello Hello world not using Qt E:\Test1>echo %errorlevel% 1 可以直接使用 System.Environment.Exit using System; class CSharpTest { static void Main() { Console.WriteLine("Hello world not using Qt"); Environment.Exit(1); } } 而不用考慮入口點函數(shù)是否返回值是int 命令行參數(shù)似乎沒什么好說的,與C++的不同處在于,參數(shù)中第一個不是程序名 using System; class CSharpTest { static void Main(string[] args) { foreach (string arg in args) Console.WriteLine(arg); } } 結(jié)果: E:\Test1>hello Qt5 Qt4 Qt3 Qt5 Qt4 Qt3 Gui程序(Windows Forms)使用Windows Forms: using System.Windows.Forms; class CSharpTest { static void Main() { MessageBox.Show("Hello World not using Qt"); } } 是這么編譯么? csc hello.cs 恩,可以,只不過有cmd窗口彈出。需要指定 csc /arget:winexe hello.cs 一般情況下,會需要使用一個Application(用來控制程序的啟動、停止、消息處理等) using System.Windows.Forms; public class Form1 : Form { public static void Main() { Application.Run(new Form1()); } public Form1() { this.DoubleClick += new System.EventHandler(form_Click); } private void form_Click(object sender, System.EventArgs e) { Application.Exit(); } } 編譯命令同上 Gui程序(WPF)這應(yīng)該是最簡單的WPF的程序了吧? using System.Windows; public class WpfTest1 { public static void Main() { MessageBox.Show("WPF Applicaiton Test"); } } 編譯(好像必須指定這些reference,fixme): E:\Test1>csc /target:winexe /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\W indowsBase.dll" hello.cs 正常情況下,我們需要有Application(和Windows.Forms中的不是同一個) using System.Windows; public class WpfTest1 { public static void Main() { Application app = new Application(); app.Run(); } } 編譯命令同上 而一旦有了xaml,似乎只靠命令行工具就很難搞定了。需要有csproj這樣的工程文件,然后用msbuild或者直接使用visual studio了。好麻煩...(xaml被編譯成了baml資源,然后嵌入到最終的dll或exe中) |
|
來自: kittywei > 《111.20-c#》