搜索
有爱,有技术,有你^_^)y
╱人◕‿‿◕人╲订下契约(注册新用户)

合作站点账号登陆

QQ登录

只需一步,快速开始

快捷导航
查看: 682|回复: 0
收起左侧

[普通教程] 【MSDNのC#系列教程】Hello World

[复制链接]

签到天数: 2 天

连续签到: 2 天

[LV.1]初来乍到

48

主题

24

好友

1万

积分

第一章

积分
11403
发表于 2012-7-20 21:47:02 | 显示全部楼层 |阅读模式

╱人◕‿‿◕人╲定下契约

您需要 登录 才可以下载或查看,没有账号?╱人◕‿‿◕人╲订下契约(注册新用户)

x
本帖最后由 羞怯的渣白君 于 2012-7-20 21:50 编辑

此为MSDN的资料源地址:http://msdn.microsoft.com/zh-cn/library/ms123401


教程

下面的示例展示编写 C#“Hello World”(世界你好)程序的几种不同方法。


示例 1


// Hello1.cs
public class Hello1
{
   public static void Main()
   {
      System.Console.WriteLine("Hello, World!");
   }
}


输出

Hello, World!


代码讨论



  • 每个 Main 方法都必须包含在一个类内(此处为 Hello1)。
  • System.Console 类包含一个 WriteLine 方法,可用于向控制台显示字符串。





示例 2
为避免程序中到处都是完全限定的类,可以使用 using 指令,如下所示:


// Hello2.cs
using System;

public class Hello2
{
   public static void Main()
   {
      Console.WriteLine("Hello, World!");
   }
}



输出

Hello, World!





示例 3

如果需要访问传递到应用程序中的命令行参数,则只需更改 Main 方法的签名以包括这些参数,如下所示。本示例对命令行参数进行计数并显示这些参数。


// Hello3.cs
// arguments: A B C D
using System;

public class Hello3
{
   public static void Main(string[] args)
   {
      Console.WriteLine("Hello, World!");
      Console.WriteLine("You entered the following {0} command line arguments:",
         args.Length );
      for (int i=0; i < args.Length; i++)
      {
         Console.WriteLine("{0}", args);
      }
   }
}



输出


Hello, World!
You entered the following 4 command line arguments:
A
B
C






示例 4

若要返回返回代码,请更改 Main 方法的签名,如下所示:

// Hello4.cs
using System;

public class Hello4
{
   public static int Main(string[] args)
   {
      Console.WriteLine("Hello, World!");
      return 0;
   }
}


输出

Hello, World!


願爾有生之日。   得見吾君臨天下
回复

使用道具 举报

本版积分规则

小黑屋|手机版|技术宅(Z站|基宅) ( 粤ICP备18082987号-1 )

GMT+8, 2025-7-3 12:33 , Processed in 0.065493 second(s), 9 queries , Redis On.

Copyright © 2018 技术宅社区

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表