轻松学习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运算符 <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类型 <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#的运算符。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |