- 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也没有问题):
如果下面这样的程序,还能直接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中)
分享到:
相关推荐
ASP.Net学习之常用模块整理 .NET C sharp
Some commonly asked interview questions regarding C# and .NET.
cefsharp62 修改版,支持framework4.0.有问题加 微信: hdawin ,请备注
相关说明太少了,做了个vb.net版的例子给大家,希望大家喜欢,其实方法也蛮简单的, 第1步,修改app.config文件。 ;"/> 第2步,在setting里面指定如下三个路径就行了 setting....
Programming With C Sharp .NET.pdf Programming With C Sharp .NET.pdf
VB.net用于安装CefSharp.WinForms的工具NuGet。大多数人认为CefSharp.WinForms只能被用于C#,实际上VB.net也可以用,而且代码比C#更精简。先上传安装CefSharp.WinForms库必须要用到的NuGet,实例和教程正在编写中。
VB.net中应用CefSharp.WinForms库构建Chrome内核浏览器实例源代码,环境为Windows 10,Visual Studio 2015,.Net FrameWorks 4.52。未包含CefSharp.WinForms的Packages文件夹以及bin下面的支持文件,请自行安装调试...
opencvsharp_ .NET Framework wrapper for OpenCV.pdf
VB.Net to C Sharp Converter 是一款将vb.net代码转化为c#代码的程序。在转化过程中,保留了原有的注释和变量名。 VB.Net to C# Converter是一款代码转换软件,将用户的VB.Net项目转换为C#语言。与别的转换软件不同...
VS2005 ASP.NET本地化学习笔记&感受 在自定义Server Control中捆绑JS文件 Step by Step 深度解析Asp.Net2.0中的Callback机制 使用 Web 标准生成 ASP.NET 2.0 Web 站点 ASP.NET 2.0基于SQLSERVER 2005的aspnetdb.mdf...
visual basic.net和visuak c sharp.net面向对象程序设计
asp.net 基础教程-c sharp案例版asp.net 基础教程-c sharp案例版asp.net 基础教程-c sharp案例版
PROGRAMMING THE MS .NET FRAMEWORK WITH MS VISUAL C SHARP .NET DELIVERY GUIDE.pdf
2.5 使用SharpDevelop构建.NET应用程序 37 2.6 使用Visual C# 2008 Express构建.NET应用程序 38 2.7 使用Visual Studio 2008构建.NET应用程序 40 2.8 其他.NET开发工具 49 2.9 小结 50 第二部分 C#核心...
资源名:C#使用S7.Net,Sharp7连接读写PLC实例源码 资源类型:程序源代码 源码说明: 里面有S7.Net.dll以及使用说明英文版的,不用区网上下载了。实现C#连接PLC1200,并读写DB块数据。项目工程师完整的,可以直接...
里面有S7.Net.dll以及使用说明英文版的,不用区网上下载了。实现C#连接PLC1200,并读写DB块数据。项目工程师完整的,可以直接学习使用。 更新:内部添加sharp7连接PLC增加读写bool变量,增加读PLC string类型,...
Programming With C Sharp .NET (VBL)
c #.net 开 发 者 手册c #.net 开 发 者 手册
C-Sharp and Asp.Net Interview Questions 主要解决c#与asp.net 注意要点
Visual CSharp .NET Programming Visual CSharp .NET Programming Visual CSharp .NET Programming