小弟刚学C #,请问在VS2008中怎样编写最简单的~hello word~的C#程序,新建项目,那些程序类型怎么选择啊

如题所述

Console Application

1. 初学者

public class HelloWorld
{
public static void Main()
{
System.Console.WriteLine("HELLO WORLD");
}
}

2. 改进的HELLO WORLD

using System;
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("HELLO WORLD");
}
}

3. 命令行形式

using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine(args[0]);
}
}

4. 构造函数

using System;
public class HelloWorld
{
public HelloWorld()
{
Console.WriteLine("HELLO WORLD");
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
}
}

5. 面向对象

using System;
public class HelloWorld
{
public void helloWorld()
{
Console.WriteLine("HELLO WORLD");
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.HelloWorld();
}
}

6. 从其他类

using System;
public class HelloWorld
{
public static void Main()
{
HelloWorldHelperClass hwh = new HelloWorldHelperClass();
hwh.writeHelloWorld();
}
}

public class HelloWorldHelperClass
{
public void writeHelloWorld()
{
Console.WriteLine("Hello World");
}
}

7. 继承

abstract class HelloWorldBase
{
public abstract void writeHelloWorld();
}

class HelloWorld : HelloWorldBase
{
public override void writeHelloWorld()
{
Console.WriteLine("Hello World");
}
}

class HelloWorldImp
{
static void Main()
{
HelloWorldBase hwb = HelloWorld;
HelloWorldBase.writeHelloWorld();
}
}

8. 静态构造函数

using System;
public class HelloWorld
{
private static string strHelloWorld;
static HelloWorld()
{
strHelloWorld = "Hello World";
}

void writeHelloWorld()
{
Console.WriteLine(strHelloWorld);
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.writeHelloWorld();
}
}

9. 异常处理

using System;
public class HelloWorld
{
public static void Main(string[] args)
{
try
{
Console.WriteLine(args[0]);
}
catch(IndexOutOfRangeException e)
{
Console.WriteLine(e.ToString());
}
}
}

10. 名字空间

using System;
namespace HelloLibrary
{
public class HelloMessage
{
public string Message
{
get
{
return "Hello, World!!!";
}
}
}
}

using System;
using HelloLibrary;

namespace HelloApplication
{
class HelloApp
{
public static void Main(string[] args)
{
HelloMessage m = new HelloMessage();
}
}
}

11. 属性

using System;
public class HelloWorld
{
public string strHelloWorld
{
get
{
return "Hello World";
}
}

public static void Main()
{
HelloWorld hw = new HelloWorld();
Console.WriteLine(cs.strHelloWorld);
}
}

12. 代理

using System;
class HelloWorld
{
static void writeHelloWorld()
{
Console.WriteLine("HelloWorld");
}

static void Main()
{
SimpleDelegate d = new SimpleDelegate(writeHelloWorld);
d();
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-24
打开VS2008-----左上角,文件,新建项目-----打开的框框中,右面有个控制台应用程序
呵呵,你的还是英文版的 是第四个 Console Application
下面是些 项目名称 存储路径 你可以自己选择;
下面这段代码就是你要的了;

namespace MyArry
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
}
}追问

谢谢啦,刚才找到了, 呵呵,有个问题啊,字符串要用双引号,双引号怎么打的啊,中文输入法输入双引号说有错误 ,我试了英文输入下用两个单引号组合还是有错误啊?

追答

英文输入法状态下的双引号呢;要在英文输入法状态下才可以的

第2个回答  2011-10-24
建一个控制台程序:
namespace MyArry
{
class Program
{

static void Main(string[] args)
{
Console.WriteLine("Hello world!");
}
}追问

我不知道怎么建控制台程序啊,控制台程序的英文明是什么啊?建项目时我该选哪个?

追答

console application

标点符号要用英文输入法。

相似回答