轻松学习C#的运算符,对C#的运算符感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的运算符。
一、字符串连接运算符(“+”)
字符串连接运算符的作用是将两个字符串连接在一起,组成一个新的字符串。在程序中出现(“提示字符”+变量),这里起字符连接作用。
用一个例子来说明字符串连接运算符的作用:
- <span style="font-size:18px;">using System; using System.Collections.Generic;
- using System.Linq; using System.Text;
- using System.Threading.Tasks;
- namespace 运算符 {
- class Program {
- static void Main(string[] args) {
- int a = 26; int b = 10;
- int c; c= a + b;
- Console.WriteLine("自然表达式a+b的值为:{0}",a);//C#中的输出格式 Console.WriteLine("{0}+{1}={2}",a,b,a+b);//C#的输出格式
- Console.WriteLine("自然表达式a+b的值为:"+a);//在这里“+”起到字符的连接作用 Console.WriteLine("a+b的返回值类型: {0}",(a+b).GetType());//显示返回值c的数据类型
- string str1 = "This is "; string str2 = "a new string";
- Console.WriteLine(str1+str2);//在这里“+”起到字符串的连接作用 Console.ReadLine();
- } }
- } </span>










