C#基础1——语法

C#基础1——语法

(一) .net  ,visual studio , c# 之间的关系  

 

 

(二)VS开发环境的介绍

 

 (三)c#语法

 

(二)c#程序

        控制台的三条指令:

        Console.WriteLine:打印输出

        Console.ReadLine:从控制台读入一行

        Console.ReadKey:控制台暂停。

例1:

 

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("hello");  
  8.             Console.ReadKey();          //按一个按键继续执行  
  9.         }  
  10.     }  
  11. }  

例2: Console.ReadLine应用

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("hello");  
  8.             string s = Console.ReadLine();//用户输入文字的时候程序是暂停的,用户输入完毕点回车,把用户输入的东西作为返回值,声明一个string类型的变量(容器)s,用s来放WriteLine函数返回的值。   
  9.             Console.WriteLine(s);  
  10.             Console.ReadKey();          //按一个按键继续执行  
  11.         }  
  12.     }  
  13. }  

例3:“+”,连接两个字符串

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("hello");  
  8.             string s = Console.ReadLine();  
  9.             Console.WriteLine(s+"你好");   
  10.   
  11.             int i1 = 10;  
  12.             int i2 = 20;  
  13.             Console.WriteLine(i1+"+"+i2+"="+(i1+i2));  
  14.   
  15.             Console.ReadKey();            
  16.         }  
  17.     }  
  18. }  


例4:占位符的形式表示“i1+i2=i3”

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int i1 = 10;  
  8.             int i2 = 20;    
  9.             //占位符从0开始,顺序就是WriteLine第二个参数开始的顺序  
  10.             Console.WriteLine("{0}+{1}={2}",i1,i2,i1+i2);     //占位符的形式表示  
  11.             Console.ReadKey();            
  12.         }  
  13.     }  
  14. }  


例4.2:

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("请输入男孩的名字");  
  8.             string boy = Console.ReadLine();  
  9.             Console.WriteLine("请输入女孩的名字");  
  10.             string girl = Console.ReadLine();  
  11.             Console.WriteLine("{0}爱{1}",boy,girl);  
  12.             Console.ReadKey();            
  13.         }  
  14.     }  
  15. }  

例5:c#从上到下一条条依次执行

       注意下面两段代码执行后第二次输出c的区别


[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int a = 3;  
  8.             int j = 5;  
  9.             int c = a + j;  
  10.             Console.WriteLine(c);  
  11.              a = 2;  
  12.             Console.WriteLine(c);  
  13.             Console.ReadKey();  
  14.         }  
  15.     }  
  16. }  


 

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int a = 3;  
  8.             int j = 5;  
  9.             int c = a + j;  
  10.             Console.WriteLine(c);  
  11.              a = 2;  
  12.             Console.WriteLine(c);  
  13.             Console.ReadKey();  
  14.         }  
  15.     }  
  16. }  

 


[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int a = 3;  
  8.             int j = 5;  
  9.             int c = a + j;  
  10.             Console.WriteLine(c);  
  11.              a = 2;  
  12.              c = a + j;  
  13.             Console.WriteLine(c);  
  14.             Console.ReadKey();  
  15.         }  
  16.     }  
  17. }  


 

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int a = 3;  
  8.             int j = 5;  
  9.             int c = a + j;  
  10.             Console.WriteLine(c);  
  11.              a = 2;  
  12.              c = a + j;  
  13.             Console.WriteLine(c);  
  14.             Console.ReadKey();  
  15.         }  
  16.     }  
  17. }  


例6:c#的注释

单行注释的表示:

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.               
  8.             int a = 3;  
  9.             int j = 5;  
  10.             int c = a + j;  
  11.             Console.WriteLine(c);  
  12.             a = 2;  
  13.             Console.WriteLine(c);    //此处很好的体现了“c#从上到下一条条依次执行  
  14.             Console.ReadKey();  
  15.         }  
  16.     }  
  17. }  


 

多行注释的表示

[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             /* 
  8.             Console.WriteLine("请输入男孩的名字"); 
  9.             string boy = Console.ReadLine(); 
  10.             Console.WriteLine("请输入女孩的名字"); 
  11.             string girl = Console.ReadLine(); 
  12.             Console.WriteLine("{0}爱{1}", boy, girl); 
  13.             */  
  14.   
  15.             int a = 3;  
  16.             int j = 5;  
  17.             int c = a + j;  
  18.             Console.WriteLine(c);  
  19.             Console.ReadKey();  
  20.         }  
  21.     }  
  22. }  

 


 

 


[csharp]  view plain copy print ?
  1. namespace ConsoleApplication1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("hello");  
  8.             string s = Console.ReadLine();//用户输入文字的时候程序是暂停的,用户输入完毕点回车,把用户输入的东西作为返回值,声明一个string类型的变量(容器)s,用s来放WriteLine函数返回的值。   
  9.             Console.WriteLine(s);  
  10.             Console.ReadKey();          //按一个按键继续执行  
  11.         }  
  12.     }  
  13. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值