轻松学习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>
输出的结果为:
二、is运算符
is运算符用于动态检查对象的运行时是否与给定类型兼容。其格式为;表达式 is 类型,运行的结果返回一个布尔值,表示“表达式”的类型if欧可通过引用转换,装箱转换或拆箱转换(其他转换不在is运算符考虑之列),然后转换为要判断的“类型”。
下面举例说明运算符的作用:
- <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)
- {
- object a = 10;
- if (a is bool)
- {
- Console.WriteLine("b是一个bool类型");
- }
- else
- {
- Console.WriteLine("b不是一个bool类型");
- }
- Console.ReadLine();
- }
- }
- }</span>
输出的结果为:b不是一个bool类型
三、as运算符
as运算符用于将一个值显式地转换(使用引用转换或装箱转换,如果执行其他的转换,应该为强制转换表达式执行这些转换)为一个给定的引用类型。其格式为:表达式 as 引用类型。当as指定的转换不能实现时,则运算结果为null。用户可通过这点判断一个表达式是否为某一数据类型。
通过一个例子来说明数组nums中的每个元素是否为字符串类型:
- <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)
- {
- object[] nums=new object[3];
- nums[0] = "123";
- nums[1] = 456;
- nums[2] = "字符串";
- for (int i = 0; i < nums.Length; i++)//遍历数组nums的所有元素
- {
- string s = nums[i] as string;//将对应的元素转换为字符串
- Console.WriteLine("nums[{0}]:",i);
- if (s!=null)
- {
- Console.WriteLine("'"+s+"'");
- }
- else
- {
- Console.WriteLine("不是一个字符串");
- }
- }
- Console.ReadLine();
- }
- }
- }
- </span>
输出的结果为:
四、当使用关系运算符比较的是两个字符的大小时的程序
- <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)
- {
- bool b1;
- char char1 = 'c';
- char char2 = 'd';
- byte[] unicode1 = Encoding.Unicode.GetBytes(new char[] { char1 });//将字符c的Unicode转换成字符串
- byte[] unicode2 = Encoding.Unicode.GetBytes(new char[] { char2 });
- Console.WriteLine("字符'c'的Unicode值为:{0}", unicode1[0]);
- Console.WriteLine("字符'd'的Unicode值为:{0}", unicode2[0]);
- b1 = char1 > char2;
- Console.WriteLine("char1>char2值为:{0}",b1);
- Console.ReadLine();
- Console.ReadLine();
- }
- }
- }</span>
输出的结果为:
五、C#中的装箱与拆箱
简单的说一下C#语言中的装箱与拆箱:
装箱:
将值类型转换为引用类型。
拆箱:
将引用类型转换为值类型。
《轻松学习C#的装箱与拆箱》
详细内容请参考本文:
以上就是本文的全部内容,希望帮助大家更好的学习了解C#的运算符。
新闻热点
疑难解答