国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

輕松學習C#的運算符

2020-01-24 01:22:39
字體:
來源:轉載
供稿:網友

 一、字符串連接運算符(“+”)
        字符串連接運算符的作用是將兩個字符串連接在一起,組成一個新的字符串。在程序中出現(“提示字符”+變量),這里起字符連接作用。
        用一個例子來說明字符串連接運算符的作用:

<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#的運算符。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 托克逊县| 皋兰县| 定远县| 高碑店市| 道孚县| 德州市| 缙云县| 南华县| 墨玉县| 昭平县| 清水河县| 竹北市| 兴安盟| 景宁| 青龙| 丰都县| 宜春市| 太原市| 越西县| 广州市| 沈阳市| 东安县| 海兴县| 丹阳市| 延津县| 从江县| 昌平区| 体育| 浦东新区| 上饶县| 凤城市| 青龙| 富宁县| 龙口市| 馆陶县| 犍为县| 伊吾县| 镇远县| 新丰县| 太和县| 深州市|