一、HelloWorld
几乎所有讲授编程语言的书籍都是从HelloWorld开始已经成为一种习惯,我们也不例外。
using System; using SC = System.Console; public class WelcomeC { public static void Main(string[] args) { SC.WriteLine("HelloWorld"); } }
二、分析程序
1.using指令
作用:
1.using 命名空间\命名空间.类
这样可以在程序中直接用命名空间中的类型或类中的方法
2.using 别名 = 命名空间\命名空间.类
这样可以个命名空间或者类型定义一个别名有利于程序简洁
错误:
示例: using System.Console.WriteLine; //error
错误using后跟的是命名空间或类型而WriteLine是Console中的方法所有错误
2.Main方法
C#程序都应有一个Main方法它是程序的起始点
注意:C#中Main方法第一个字母要大写
3.Console类
位于System命名空间中提供了标准输入流、输出流。主要学习一下WriteLine()和ReadLine()方法。
WriteLine()方法
HelloWorld的另外显示方式:
using System; public class WelcomeC { public static void Main(string[] args) { string str1 = "Hello"; string str2 = "World"; int i1 = 100; Console.WriteLine(str1+str2+i1+"%"); Console.WriteLine("{0}{1}{2}%",str1,str2,i1); Console.ReadKey(); } }其中可以用+来连接字符串 用{}来当做占位符大括号中的数字用来表示后面元素的顺序注意:这里之所以加上一个整数是用来说明WriteLine方法中参数是string类型的之所以能打印值类型是因为调用了ToString()方法此方法在Object类中定义
ReadLine()方法从标准输入流中取下一行字符注意:他的返回值是String类型如果想接收一个int类型用于数值计算必须进行类型转换否则会报错
三、总结
- using指令:基本上是每一个程序的开头
- Main()方法:程序的入口
- System.Console类:其中定义了标准输入输出流重要的有WriteLine()和readLine()方法
浙公网安备 33010602011771号