首页 > 编程 > C# > 正文

轻松学习C#的运算符

2019-10-29 21:35:45
字体:
来源:转载
供稿:网友

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

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

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

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

 

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

输出的结果为:

轻松学习C#的运算符

二、is运算符

is运算符用于动态检查对象的运行时是否与给定类型兼容。其格式为;表达式 is 类型,运行的结果返回一个布尔值,表示“表达式”的类型if欧可通过引用转换,装箱转换或拆箱转换(其他转换不在is运算符考虑之列),然后转换为要判断的“类型”。

下面举例说明运算符的作用:

 

  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.  
  7. namespace 运算符  
  8. {  
  9. class Program  
  10. {  
  11. static void Main(string[] args)  
  12. {  
  13. object a = 10;  
  14. if (a is bool)  
  15. {  
  16. Console.WriteLine("b是一个bool类型");  
  17. }  
  18. else 
  19. {  
  20. Console.WriteLine("b不是一个bool类型");  
  21. }  
  22. Console.ReadLine();  
  23. }  
  24. }  
  25. }</span>  

输出的结果为:b不是一个bool类型

三、as运算符

as运算符用于将一个值显式地转换(使用引用转换或装箱转换,如果执行其他的转换,应该为强制转换表达式执行这些转换)为一个给定的引用类型。其格式为:表达式 as 引用类型。当as指定的转换不能实现时,则运算结果为null。用户可通过这点判断一个表达式是否为某一数据类型。

通过一个例子来说明数组nums中的每个元素是否为字符串类型:

 

 
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.  
  7. namespace 运算符  
  8. {  
  9. class Program  
  10. {  
  11. static void Main(string[] args)  
  12. {  
  13. object[] nums=new object[3];  
  14. nums[0] = "123";  
  15. nums[1] = 456;  
  16. nums[2] = "字符串";  
  17. for (int i = 0; i < nums.Length; i++)//遍历数组nums的所有元素  
  18. {  
  19. string s = nums[i] as string;//将对应的元素转换为字符串  
  20. Console.WriteLine("nums[{0}]:",i);  
  21. if (s!=null)  
  22. {  
  23. Console.WriteLine("'"+s+"'");  
  24. }  
  25. else 
  26. {  
  27. Console.WriteLine("不是一个字符串");  
  28. }  
  29. }  
  30. Console.ReadLine();  
  31. }  
  32. }  
  33. }  
  34. </span>  

输出的结果为:

轻松学习C#的运算符

四、当使用关系运算符比较的是两个字符的大小时的程序

 

  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.  
  7. namespace 运算符  
  8. {  
  9. class Program  
  10. {  
  11. static void Main(string[] args)  
  12. {  
  13. bool b1;  
  14. char char1 = 'c';  
  15. char char2 = 'd';  
  16. byte[] unicode1 = Encoding.Unicode.GetBytes(new char[] { char1 });//将字符c的Unicode转换成字符串  
  17. byte[] unicode2 = Encoding.Unicode.GetBytes(new char[] { char2 });  
  18. Console.WriteLine("字符'c'的Unicode值为:{0}", unicode1[0]);  
  19. Console.WriteLine("字符'd'的Unicode值为:{0}", unicode2[0]);  
  20. b1 = char1 > char2;  
  21. Console.WriteLine("char1>char2值为:{0}",b1);  
  22. Console.ReadLine();  
  23. Console.ReadLine();  
  24. }  
  25. }  
  26. }</span>  

输出的结果为:

轻松学习C#的运算符

五、C#中的装箱与拆箱

简单的说一下C#语言中的装箱与拆箱:

装箱:

将值类型转换为引用类型。

拆箱:

将引用类型转换为值类型。

《轻松学习C#的装箱与拆箱》

详细内容请参考本文:

以上就是本文的全部内容,希望帮助大家更好的学习了解C#的运算符。


注:相关教程知识阅读请移步到c#教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表