C#基础1——语法
(一) .net ,visual studio , c# 之间的关系
(二)VS开发环境的介绍
(三)c#语法
(二)c#程序
控制台的三条指令:
Console.WriteLine:打印输出
Console.ReadLine:从控制台读入一行
Console.ReadKey:控制台暂停。
例1:
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("hello");
- Console.ReadKey(); //按一个按键继续执行
- }
- }
- }
例2: Console.ReadLine应用
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("hello");
- string s = Console.ReadLine();//用户输入文字的时候程序是暂停的,用户输入完毕点回车,把用户输入的东西作为返回值,声明一个string类型的变量(容器)s,用s来放WriteLine函数返回的值。
- Console.WriteLine(s);
- Console.ReadKey(); //按一个按键继续执行
- }
- }
- }
例3:“+”,连接两个字符串
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("hello");
- string s = Console.ReadLine();
- Console.WriteLine(s+"你好");
- int i1 = 10;
- int i2 = 20;
- Console.WriteLine(i1+"+"+i2+"="+(i1+i2));
- Console.ReadKey();
- }
- }
- }
例4:占位符的形式表示“i1+i2=i3”
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int i1 = 10;
- int i2 = 20;
- //占位符从0开始,顺序就是WriteLine第二个参数开始的顺序
- Console.WriteLine("{0}+{1}={2}",i1,i2,i1+i2); //占位符的形式表示
- Console.ReadKey();
- }
- }
- }
例4.2:
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("请输入男孩的名字");
- string boy = Console.ReadLine();
- Console.WriteLine("请输入女孩的名字");
- string girl = Console.ReadLine();
- Console.WriteLine("{0}爱{1}",boy,girl);
- Console.ReadKey();
- }
- }
- }
例5:c#从上到下一条条依次执行
注意下面两段代码执行后第二次输出c的区别
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a = 3;
- int j = 5;
- int c = a + j;
- Console.WriteLine(c);
- a = 2;
- Console.WriteLine(c);
- Console.ReadKey();
- }
- }
- }
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a = 3;
- int j = 5;
- int c = a + j;
- Console.WriteLine(c);
- a = 2;
- Console.WriteLine(c);
- Console.ReadKey();
- }
- }
- }
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a = 3;
- int j = 5;
- int c = a + j;
- Console.WriteLine(c);
- a = 2;
- c = a + j;
- Console.WriteLine(c);
- Console.ReadKey();
- }
- }
- }
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a = 3;
- int j = 5;
- int c = a + j;
- Console.WriteLine(c);
- a = 2;
- c = a + j;
- Console.WriteLine(c);
- Console.ReadKey();
- }
- }
- }
例6:c#的注释
单行注释的表示:
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a = 3;
- int j = 5;
- int c = a + j;
- Console.WriteLine(c);
- a = 2;
- Console.WriteLine(c); //此处很好的体现了“c#从上到下一条条依次执行
- Console.ReadKey();
- }
- }
- }
多行注释的表示
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- /*
- Console.WriteLine("请输入男孩的名字");
- string boy = Console.ReadLine();
- Console.WriteLine("请输入女孩的名字");
- string girl = Console.ReadLine();
- Console.WriteLine("{0}爱{1}", boy, girl);
- */
- int a = 3;
- int j = 5;
- int c = a + j;
- Console.WriteLine(c);
- Console.ReadKey();
- }
- }
- }
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("hello");
- string s = Console.ReadLine();//用户输入文字的时候程序是暂停的,用户输入完毕点回车,把用户输入的东西作为返回值,声明一个string类型的变量(容器)s,用s来放WriteLine函数返回的值。
- Console.WriteLine(s);
- Console.ReadKey(); //按一个按键继续执行
- }
- }
- }