C#部分文法總結

來源:互聯網
上載者:User

標籤:搜尋   number   ide   www   聲明   names   val   try   常用   

例如:using System; 一般都會出現在*.cs中。

2.using別名。using + 別名 = 包括詳細命名空間資訊的具體的類型。 
這種做法有個好處就是當同一個cs引用了兩個不同的命名空間,但兩個命名空間都包括了一個相同名字的類型的時候。當需要用到這個類型的時候,就每個地方都要用詳細命名空間的辦法來區分這些相同名字的類型。而用別名的方法會更簡潔,用到哪個類就給哪個類做別名聲明就可以了。注意:並不是說兩個名字重複,給其中一個用了別名,另外一個就不需要用別名了,如果兩個都要使用,則兩個都需要用using來定義別名的。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 using System;using aClass = NameSpace1.MyClass;using bClass = NameSpace2.MyClass; namespace NameSpace1 {    public class MyClass     {        public override string ToString()         {            return "You are in NameSpace1.MyClass";        }    }} namespace NameSpace2 {    class MyClass     {        public override string ToString()         {            return "You are in NameSpace2.MyClass";        }    }} namespace testUsing{    using NameSpace1;    using NameSpace2;    /// <summary>    /// Class1 的摘要說明。    /// </summary>    class Class1    {        /// <summary>        /// 應用程式的主進入點。        /// </summary>        [STAThread]        static void Main(string[] args)        {            //            // TODO: 在此處添加代碼以啟動應用程式            //             aClass my1 = new aClass();            Console.WriteLine(my1);            bClass my2 = new bClass();            Console.WriteLine(my2);            Console.WriteLine("Press any key");            Console.Read();        }    }}

3.using語句,定義一個範圍,在範圍結束時處理對象。 
情境: 
當在某個程式碼片段中使用了類的執行個體,而希望無論因為什麼原因,只要離開了這個程式碼片段就自動調用這個類執行個體的Dispose。 
要達到這樣的目的,用try...catch來捕捉異常也是可以的,但用using也很方便。

1234 using (Class1 cls1 = new Class1(), cls2 = new Class1()){  // the code using cls1, cls2} // call the Dispose on cls1 and cls2
 5. 數組的操作

Array.Sort(nums/*數組名稱*/);

Array.Reverse(nums/*數組名稱*/);

6. IndexOf
        public static void UseIndexOf()        {            string str = "hello cat. hi boatlet. How are you cat? Fine, Thanks!";                                             //搜尋字串,初始位置            int index = str.IndexOf("cat", 0);            int i = 1;            if(index != -1)            {                Console.WriteLine("cat 第{0}次出現位置為{1}", i, index);                while(index != -1)                {                    index = str.IndexOf("cat", index + 1);                    i++;                    if(index == -1)                    {                        break;                    }                    Console.WriteLine("cat 第{0}次出現位置為{1}", i, index);                }            }

  

 

C#部分文法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.