轻松学习C#的运算符

2019-12-30 11:22:09刘景俊

轻松学习C#的运算符,对C#的运算符感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的运算符。

一、字符串连接运算符(“+”)

字符串连接运算符的作用是将两个字符串连接在一起,组成一个新的字符串。在程序中出现(“提示字符”+变量),这里起字符连接作用。

用一个例子来说明字符串连接运算符的作用:

 

 
  1. <span style="font-size:18px;">using System;   using System.Collections.Generic;  
  2. using System.Linq;   using System.Text;  
  3. using System.Threading.Tasks;    
  4. namespace 运算符   {  
  5. class Program   {  
  6. static void Main(string[] args)   {  
  7. int a = 26;   int b = 10;  
  8. int c;   c= a + b;  
  9. Console.WriteLine("自然表达式a+b的值为:{0}",a);//C#中的输出格式   Console.WriteLine("{0}+{1}={2}",a,b,a+b);//C#的输出格式  
  10. Console.WriteLine("自然表达式a+b的值为:"+a);//在这里“+”起到字符的连接作用   Console.WriteLine("a+b的返回值类型: {0}",(a+b).GetType());//显示返回值c的数据类型  
  11. string str1 = "This is ";   string str2 = "a new string";  
  12. Console.WriteLine(str1+str2);//在这里“+”起到字符串的连接作用   Console.ReadLine();  
  13. }   }  
  14. }   </span>