`
izuoyan
  • 浏览: 8953163 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

C Sharp与.net学习笔记(一)

 
阅读更多
  • C#和.net都是零基础,需要一到两周先恶补一点东西,特此记录一下2012.01.11

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

有没有问题?

  • 返回值何在?
  • 命令行参数如何获取?
  • 入口点必须为Main?如果多个类的话,怎么找到入口的
  • ...

入口点

可用的入口点(需要时某个类的静态成员函数,MSDN中 Hello World in Visual C#中提到必须是public,但似乎private也没有问题):

  • staticvoidMain()

  • staticintMain()

  • staticvoidMain(string[]args)

  • staticintMain(string[]args)

如果下面这样的程序,还能直接cschello.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

选择入口点。

如果一个类内多个入口点函数,似乎就没办法了(fixme)

返回值

  • 返回值是 int 的入口点,然后直接使用return即可

using System;

class CSharpTest
{
    static int Main()
    {
        Console.WriteLine("Hello world not using Qt");
        return 1;
    }
}

结果

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);
    }
}

而不用考虑入口点函数是否返回值是int

命令行参数

似乎没什么好说的,与C++的不同处在于,参数中第一个不是程序名

using System;

class CSharpTest
{
    static void Main(string[] args)
    {
        foreach (string arg in args)
            Console.WriteLine(arg);
    }
}

结果:

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)

这应该是最简单的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中)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics