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

首頁 > 編程 > .NET > 正文

Visual C# .NET 入門

2024-07-10 13:00:11
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • microsoft corporation

    適用于:microsoft visual c# .net

    摘要:visual c# .net 是功能強大的編程語言 visual studio 套件的最新成員。visual c# .net 入門指南通過實現一個簡單的 quicksort 算法,帶您領略如何構建 visual c# .net 項目。

    下載 quicksort_visual_csharp_.net.exe。

    本頁內容

    簡介
    步驟 1. 開始項目
    步驟 2. hello, world!
    步驟 3. 程序結構
    步驟 4. 控制臺輸入
    步驟 5. 使用數組
    步驟 6. 文件輸入/輸出
    步驟 7. 創建函數
    步驟 8. 使用調試器
    小結
    補遺:quicksort c# .net 的源代碼
    補遺:關于 quicksort c# .net

    簡介
    visual c# .net 是 visual studio 系列中的最新成員。這種新語言基于 c/c++,但它深化了更容易地使用面向組件編程的發展方向。c/c++ 程序員應該非常熟悉它的語法。

    下面的示例應用程序示范了如何構建一個簡單的實現 quicksort 算法的 c# 項目。它包括了 c# 程序的基本組成部分:讀/寫控制臺和文件、創建函數和使用基本數組。

    這些入門指南并不打算涵蓋該編程語言的所有方面。它們只是您探索這種語言的一個起點。我們鼓勵您按照本教程的說明執行,因為它包括了 quicksort 應用程序的各個不同部分。您還可以獲得完整的源代碼和項目文件。

    建議的要求

    編譯此示例應用程序需要 visual studio.net(測試版 2 或更高版本)。關于 c/c++ 的知識是有幫助的但不是必需的。


    步驟 1. 開始項目
    visual studio 中的開發工作以解決方案的形式進行組織,每個解決方案包含一個或多個項目。在本教程中,我們創建的解決方案包含一個 c# 項目。

    創建一個新項目
    1.
    在 visual studio .net 環境中,從菜單中選擇 file | new | project。




    2.
    在左側選擇 visual c#projects,然后在右側選擇 console application。




    3.
    指定項目的名稱,然后輸入創建項目的位置。visual studio 會自動創建項目目錄。




    4.
    單擊 ok,那么現在就正式開始了!


    visual c# 解決方案
    visual studio.net 已經創建了含有一個簡單 visual c# 項目的解決方案。該項目包含兩個文件:assemblyinfo.cs 和 class1.cs。

    接下來的幾步驟將討論這些不同的文件以及如何編譯該項目。


    步驟 2. hello, world!
    很遺憾,但我們仍然無法抵御這種誘惑……我們還是不得不完成一個基于 c# 的經典"hello, world!"應用程序,這個應用程序最初是用 c 語言編寫的。

    修改源代碼
    1.
    在 solution explorer 中雙擊文件"class1.cs"。可以通過"view"菜單來顯示 solution explorer。

    2.
    更改預生成的模板 (class1.cs),如下面以斜體突出顯示的 代碼所示。

    using system;
    namespace quicksort
    {
    ///
    /// summary description for class1.
    ///
    class class1
    {
    static void main(string[] args)
    {
    //
    // todo: add code to start application here
    //
    console.writeline ("hello, c#.net world!");
    }
    }
    }


    3.
    注意,當您鍵入代碼時,visual studio 將為您提示類和函數的名稱(因為 .net 框架發布了這種類型信息)。







    編譯應用程序
    1.
    既然您已經完成了修改,就可以通過在 build 菜單中簡單地選擇 build 來編譯 visual c# 項目。




    2.
    來自 c# 編譯器的錯誤和消息會在 output 窗口中顯示。如果沒有錯誤,則可以通過單擊 debug 菜單下的 start without debugging 來運行 hello world 應用程序。





    程序輸出
    在 visual c# 中運行 hello world 示例應用程序時,輸出結果的屏幕截圖如下:




    理解更改
    system.console 類的 writeline() 函數打印傳遞給它的字符串,其后緊跟一行新的字符。此函數可以接受許多其他數據類型(包括整型和浮點型)的參數。

    在程序加載完成后,控制就傳遞給 main() 函數。這就是我們在該過程中插入對 writeline() 調用的原因。


    步驟 3. 程序結構
    既然我們已經構建了一個簡單的 hello world 應用程序,那么就讓我們停下來分析一下 visual c# 應用程序的基本組成部分。

    源代碼注釋
    字符 // 將行的剩余部分標記為一個注釋,這樣 c# 編譯器就會忽略它。另外,/* 和 */ 之間的代碼也會被當作注釋。

    // this line is ignored by the compiler.
    /* this block of text is also
    ignored by the visual c# compiler. */

    using 指令
    .net 框架為開發人員提供了許多有用的類。例如,console 類處理對控制臺窗口的輸入和輸出。這些類是按照層次樹的形式組織的。console 類的完全限定名實際上是 system.console。其他的類包括 system.io.filestream 和 system.collections.queue。

    using 指令允許您在不使用完全限定名的情況下引用命名空間中的類。以斜體突出顯示的 代碼應用了 using 指令。

    using system;
    class class1
    {
    static void main(string[] args)
    {
    system.console.writeline ("hello, c#.net world!");
    console.writeline ("hello, c#.net world!");
    }
    }

    類聲明
    與 c++ 或 visual basic 不同,visual c# 中的所有函數都必須封裝在一個類中。class 語句聲明一個新的 c# 類。就 hello world 應用程序來說,class1 類包含一個函數,即 main() 函數。如果用一個 namespace 塊將類的定義括起來,就可以把類組織為諸如 msdnaa.quicksortapp 這樣的層次。

    在本入門指南中,我們并不打算深入地介紹類,但是我們將為您簡要概述為什么類是我們的示例應用程序的一部分。

    main() 函數
    在應用程序加載到內存之后,main() 函數就會接收控制,因此,應該將應用程序啟動代碼放在此函數中。傳遞給程序的命令行參數存儲在 args 字符串數組中。


    步驟 4. 控制臺輸入
    現在,我們將繼續編寫 quicksort 應用程序。我們需要做的第一件事就是提示用戶提供輸入和輸出文件。

    修改源代碼
    更改 c# 源文件 (class1.cs),如下面以斜體突出顯示的代碼所示。其他的差異(如類名)可忽略不計。

    // import namespaces
    using system;
    // declare namespace
    namespace msdnaa
    {
    // declare application class
    class quicksortapp
    {
    // application initialization
    static void main (string[] szargs)
    {
    // describe program function
    console.writeline ("quicksort c#.net sample application/n");
    // prompt user for filenames
    console.write ("source: ");
    string szsrcfile = console.readline ();
    console.write ("output: ");
    string szdestfile = console.readline ();
    }
    }
    }

    從控制臺進行讀取
    console 類的 readline() 方法提示用戶輸入,并返回輸入的字符串。它會自動地為字符串處理內存分配,由于使用了 .net 垃圾回收器,您不需要做任何釋放內存的工作。

    程序輸出
    從菜單中選擇 debug | start without debugging 來運行程序。這是到此為止來自 quicksort 應用程序的輸出的屏幕截圖。





    步驟 5. 使用數組
    在對從輸入讀取的行進行排序之前,程序需要將其存儲到一個數組中。我們將簡要討論可實現對象數組的 .net 基類的用法。

    修改源代碼
    更改 c# 源文件 (class1.cs),如下面以斜體突出顯示的代碼所示。其他的差異(如類名)可忽略不計。

    // import namespaces
    using system;
    using system.collections;
    // declare namespace
    namespace msdnaa
    {
    // declare application class
    class quicksortapp
    {
    // application initialization
    static void main (string[] szargs)
    {
    // describe program function
    console.writeline ("quicksort c#.net sample application/n");
    // prompt user for filenames
    console.write ("source: ");
    string szsrcfile = console.readline ();
    console.write ("output: ");
    string szdestfile = console.readline ();
    // todo: read contents of source file
    arraylist szcontents = new arraylist ();
    }
    }
    }

    使用 arraylist 類
    我們將導入 system.collections 命名空間,這樣我們就可以直接引用 arraylist。此類實現大小可動態調整的對象數組。要插入新的元素,可以簡單地將對象傳遞到 arraylist 類的 add() 方法。新的數組元素將引用原始的對象,而垃圾回收器將處理它的釋放。

    string szelement = "insert-me";
    arraylist szarray = new arraylist ();
    szarray.add (szelement);

    要檢索現有的元素,請將所需元素的索引傳遞給 item() 方法。另外,作為一種簡寫形式,還可以使用方括號 operator [],它實際上映射到 item() 方法。

    console.writeline (szarray[2]);
    console.writeline (szarray.item (2));

    arraylist 類中還有許多其他方法,但是插入和檢索都是我們需要在此示例中使用的。請查閱 msdn 庫以獲得完整的參考指南。

    步驟 6. 文件輸入/輸出
    現在,讓我們來實現讀取輸入文件和寫入輸出文件。我們將每一行讀取到一個字符串數組中,然后輸出該字符串數組。在下一步中,我們將使用 quicksort 算法來對該數組進行排序。

    修改源代碼
    更改 c# 源文件 (class1.cs),如下面以斜體突出顯示的代碼所示。其他的差異(如類名)可忽略不計。

    // import namespaces
    using system;
    using system.collections;
    using system.io;
    // declare namespace
    namespace msdnaa
    {
    // declare application class
    class quicksortapp
    {
    // application initialization
    static void main (string[] szargs)
    {
    ... ... ...
    // read contents of source file
    string szsrcline;
    arraylist szcontents = new arraylist ();
    filestream fsinput = new filestream (szsrcfile, filemode.open,
    fileaccess.read);
    streamreader srinput = new streamreader (fsinput);
    while ((szsrcline = srinput.readline ()) != null)
    {
    // append to array
    szcontents.add (szsrcline);
    }
    srinput.close ();
    fsinput.close ();
    // todo: pass to quicksort function
    // write sorted lines
    filestream fsoutput = new filestream (szdestfile,
    filemode.create, fileaccess.write);
    streamwriter sroutput = new streamwriter (fsoutput);
    for (int nindex = 0; nindex < szcontents.count; nindex++)
    {
    // write line to output file
    sroutput.writeline (szcontents[nindex]);
    }
    sroutput.close ();
    fsoutput.close ();
    // report program success
    console.writeline ("/nthe sorted lines have been written./n/n");
    }
    }
    }

    從源文件進行讀取
    使用 filestream 類打開源文件,然后加入 streamreader 類,這樣我們就可以使用它的 readline() 方法了。現在,我們調用 readline() 方法,直到它返回 null,這表示到達文件結尾。在循環過程中,我們將讀取的行存儲到字符串數組中,然后關閉這兩個對象。




    寫入輸出文件
    假設已經用 quicksort 對字符串數組進行了排序,接下來要做的事情就是輸出數組的內容。按照同樣的方式,我們將 streamwriter 對象附加到 filestream 對象上。這使得我們可以使用 writeline() 方法,該方法能夠很方便地模仿 console 類的行為。一旦遍歷了數組,我們便可以象前面一樣關閉這兩個對象。





    步驟 7. 創建函數
    最后一步就是創建一個函數來在字符串數組中運行 quicksort。我們將此函數放到應用程序類 quicksortapp 之中。

    修改源代碼
    更改 c# 源文件 (class1.cs),如下面以斜體突出顯示的 代碼所示。其他的差異(如類名)可忽略不計。

    // import namespaces
    using system;
    using system.collections;
    using system.io;
    // declare namespace
    namespace msdnaa
    {
    // declare application class
    class quicksortapp
    {
    // application initialization
    static void main (string[] szargs)
    {
    ... ... ...
    // pass to quicksort function
    quicksort (szcontents, 0, szcontents.count - 1);
    ... ... ...
    }
    // quicksort implementation
    static void quicksort (arraylist szarray, int nlower, int nupper)
    {
    // check for non-base case
    if (nlower < nupper)
    {
    // split and sort partitions
    int nsplit = partition (szarray, nlower, nupper);
    quicksort (szarray, nlower, nsplit - 1);
    quicksort (szarray, nsplit + 1, nupper);
    }
    }
    // quicksort partition implementation
    static int partition (arraylist szarray, int nlower, int nupper)
    {
    // pivot with first element
    int nleft = nlower + 1;
    string szpivot = (string) szarray[nlower];
    int nright = nupper;
    // partition array elements
    string szswap;
    while (nleft <= nright)
    {
    // find item out of place
    while (nleft <= nright)
    {
    if (((string) szarray[nleft]).compareto (szpivot) > 0)
    break;
    nleft = nleft + 1;
    }
    while (nleft <= nright)
    {
    if (((string) szarray[nright]).compareto (szpivot) <= 0)
    break;
    nright = nright - 1;
    }
    // swap values if necessary
    if (nleft < nright)
    {
    szswap = (string) szarray[nleft];
    szarray[nleft] = szarray[nright];
    szarray[nright] = szswap;
    nleft = nleft + 1;
    nright = nright - 1;
    }
    }
    // move pivot element
    szswap = (string) szarray[nlower];
    szarray[nlower] = szarray[nright];
    szarray[nright] = szswap;
    return nright;
    }
    }
    }

    quicksort() 函數
    這個函數需要三個參數:對數組的引用、下界和上界。它調用 partition() 函數將數組分成兩部分,其中一部分包含 pivot 值之前的所有字符串,另一部分包含 pivot 值之后的所有字符串。然后,它調用自身來對每個部分進行排序。

    上面修改中的注釋應該說明了每個代碼塊的作用。唯一的新概念就是 compareto() 方法的使用,該方法是 string 類的成員,并且應該是自說明的。

    運行 quicksort 應用程序
    這一步完成 quicksort c# 示例應用程序。現在,可以構建項目并運行應用程序。需要提供一個示例文本文件,以供其進行排序。將該文件放在與 exe 文件相同的目錄中。




    程序輸出
    下面是已完成的 quicksort c# .net 示例應用程序的輸出。可以查看示例輸入文件 'example.txt' 和輸出文件 'output.txt'。





    步驟 8. 使用調試器
    調試器是診斷程序問題的一個必不可少的工具。我們覺得有必要在本入門指南中對其進行介紹。這最后一步將向您展示如何走查程序和使用諸如 quickwatch 這樣的功能。

    設置斷點
    當程序在調試器中運行時,斷點會暫停程序的執行,從而使開發人員能夠控制調試器。要設置斷點,請右鍵單擊您想要程序暫停的行,然后單擊 insertbreakpoint,如下所示。




    注:帶有斷點的行以紅色突出顯示。通過再次右鍵單擊該行并選擇 remove breakpoint 可以刪除斷點。

    單步調試程序
    既然設置了斷點(最好是在前面所示的行中),就讓我們在調試器中運行程序。在 debug 菜單中,選擇 start 而不是同前面一樣選擇 start without debugging。這樣就在調試器中啟動了程序,并因而激活了斷點。

    一旦程序遇到斷點,調試器便會接收程序的控制。這時會有一個箭頭指向當前執行的行。




    要單步調試一行代碼,可以選擇 debug | step over 并觀察光標是否移到下一行。debug | step into 命令允許您單步執行將要調用的函數。進行兩次 step over 之后的屏幕如下所示。




    如果想要程序在遇到下一個斷點、遇到異常或退出之前繼續執行,請從菜單中選擇 debug | continue。

    檢查變量值
    當您可以控制調試器時,可將鼠標指針移到變量上以獲得它的基本值。




    您也可以右鍵單擊變量,然后從上下文菜單中選擇 quickwatch。quickwatch 將為您提供關于某些變量(如 arraylist 對象)的更多詳細信息。




    其他調試器工具
    visual studio 調試器具有許多其他工具(例如 call stack 查看器)的功能,可以使用此調試器來查看到此為止調用的函數。還可以獲得內存轉儲和關于進程中線程的信息。我們鼓勵您使用這些功能強大的調試工具。





    小結
    本入門指南旨在幫助您用 visual studio 構建一個簡單的 c# 項目。它無法進行全面的介紹。我們鼓勵您查詢關于 c# 和 .net 的其他資源,以便更多地學習這些技術。在完成本教程之后,您至少有了一個可用的項目,在您研究 visual c# 時,可以從修改此這些代碼開始。

    為了方便起見,我們提供了完整的源程序和項目文件。您可以通過本文檔頂部的目錄來訪問它們。

    其他資源
    我們強烈推薦下面這些關于 c# 和 .net 平臺的書籍。它們是開發人員嘗試學習這些新技術的有益資源。

    &#8226; archer, tom.inside c#.redmond:microsoft press, 2001.

    &#8226; deitel, harvey.c#:how to program.upper saddle river, nj:prentice hall, 2001.

    &#8226; gunnerson, eric.a programmer's introduction to c#.new york:apress, 2000.

    &#8226; platt, david.introducing microsoft .net.redmond:microsoft press, 2001.



    補遺:quicksort c# .net 的源代碼
    下面是 quicksort c# .net 示例應用程序的完整源代碼。您可以復制、使用和分發這些代碼(無版權費)。注意,這些源代碼以"原樣"提供并且不作任何保證。

    //
    // quicksort c# .net sample application
    // copyright 2001-2002 microsoft corporation. all rights reserved.
    //
    // msdn academic alliance [http://www.msdn.microsoft.com/academic]
    // this sample is part of a vast collection of resources we developed for
    // faculty members in k-12 and higher education. visit the msdn aa web site for more!
    // the source code is provided "as is" without warranty.
    //
    // import namespaces
    using system;
    using system.collections;
    using system.io;
    // declare namespace
    namespace msdnaa
    {
    // declare application class
    class quicksortapp
    {
    // application initialization
    static void main (string[] szargs)
    {
    // print startup banner
    console.writeline ("/nquicksort c#.net sample application");
    console.writeline ("copyright (c)2001-2002 microsoft corporation. all rights reserved./n");
    console.writeline ("msdn academic alliance [http://www.msdnaa.net/]/n");
    // describe program function
    console.writeline ("this example demonstrates the quicksort algorithm by reading an input file,");
    console.writeline ("sorting its contents, and writing them to a new file./n");
    // prompt user for filenames
    console.write ("source: ");
    string szsrcfile = console.readline ();
    console.write ("output: ");
    string szdestfile = console.readline ();
    // read contents of source file
    string szsrcline;
    arraylist szcontents = new arraylist ();
    filestream fsinput = new filestream (szsrcfile, filemode.open, fileaccess.read);
    streamreader srinput = new streamreader (fsinput);
    while ((szsrcline = srinput.readline ()) != null)
    {
    // append to array
    szcontents.add (szsrcline);
    }
    srinput.close ();
    fsinput.close ();
    // pass to quicksort function
    quicksort (szcontents, 0, szcontents.count - 1);
    // write sorted lines
    filestream fsoutput = new filestream (szdestfile, filemode.create, fileaccess.write);
    streamwriter sroutput = new streamwriter (fsoutput);
    for (int nindex = 0; nindex < szcontents.count; nindex++)
    {
    // write line to output file
    sroutput.writeline (szcontents[nindex]);
    }
    sroutput.close ();
    fsoutput.close ();
    // report program success
    console.writeline ("/nthe sorted lines have been written to the output file./n/n");
    }
    // quicksort implementation
    private static void quicksort (arraylist szarray, int nlower, int nupper)
    {
    // check for non-base case
    if (nlower < nupper)
    {
    // split and sort partitions
    int nsplit = partition (szarray, nlower, nupper);
    quicksort (szarray, nlower, nsplit - 1);
    quicksort (szarray, nsplit + 1, nupper);
    }
    }
    // quicksort partition implementation
    private static int partition (arraylist szarray, int nlower, int nupper)
    {
    // pivot with first element
    int nleft = nlower + 1;
    string szpivot = (string) szarray[nlower];
    int nright = nupper;
    // partition array elements
    string szswap;
    while (nleft <= nright)
    {
    // find item out of place
    while (nleft <= nright && ((string) szarray[nleft]).compareto (szpivot) <= 0)
    nleft = nleft + 1;
    while (nleft <= nright && ((string) szarray[nright]).compareto (szpivot) > 0)
    nright = nright - 1;
    // swap values if necessary
    if (nleft < nright)
    {
    szswap = (string) szarray[nleft];
    szarray[nleft] = szarray[nright];
    szarray[nright] = szswap;
    nleft = nleft + 1;
    nright = nright - 1;
    }
    }
    // move pivot element
    szswap = (string) szarray[nlower];
    szarray[nlower] = szarray[nright];
    szarray[nright] = szswap;
    return nright;
    }
    }
    }

    補遺:關于 quicksort c# .net
    為了演示 quicksort visual c# .net 示例應用程序實際是如何運行的,我們提供了編譯好的可執行文件。您可以通過編譯這些項目文件來創建自己的可執行文件。單擊 quicksort_visual_csharp_.net.exe,下載源代碼項目文件和可執行文件包。

    使用應用程序
    啟動 command prompt(從"開始"菜單運行"cmd.exe")。使用 cd 命令將目錄更改為可執行文件所在的目錄。然后運行"quicksort.exe"。

    程序將提示您提供輸入和輸出文件的名稱。任何包含多行的文本文件均可使用。如果需要,可以使用記事本來創建一個此類文件。然后,該程序將對輸入文件的內容進行排序,并且將其寫入輸出文件。

    示例程序輸出
    下面是來自此 quicksort c# .net 應用程序的一個實例的輸出。此示例演示了 quicksort 算法,方法是讀取輸入文件、對文件的內容進行排序,然后將其寫入新的文件。用戶輸入的文本以下劃線標記。

    您可以查看下面的示例輸入文件 'example.txt' 和輸出文件 'output.txt'。

    quicksort c# .net sample application
    copyright (c)2001-2002 microsoft corporation. all rights reserved.
    msdn academic alliance [http://www.msdn.microsoft.com/academic]
    this example demonstrates the quicksort algorithm by reading an input file,
    sorting its contents, and writing them to a new file.
    source: example.txt
    output: output.txt
    the sorted lines have been written to the output file.

    查看示例輸入文件"example.txt":

    visual c#
    windows embedded
    javascript
    speech api
    asp.net
    vbscript
    windows media
    visual basic
    .net framework
    biztalk server
    xml parser
    internet explorer
    visual c#
    sql server
    windows xp
    directx api

    查看示例輸出文件"output.txt":

    .net framework
    asp.net
    biztalk server
    directx api
    internet explorer
    javascript
    speech api
    sql server
    vbscript
    visual basic
    visual c#
    visual c#
    windows embedded
    windows media
    windows xp
    xml parser

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 红河县| 五河县| 东阳市| 攀枝花市| 苏州市| 黔南| 思南县| 什邡市| 竹山县| 麻阳| 沭阳县| 阳江市| 富民县| 磴口县| 夏津县| 阿城市| 武汉市| 长垣县| 顺平县| 昌平区| 平陆县| 尖扎县| 炉霍县| 贵南县| 依安县| 固始县| 黔南| 徐汇区| 枝江市| 九寨沟县| 新和县| 塘沽区| 维西| 固原市| 多伦县| 习水县| 贵溪市| 个旧市| 蒲城县| 绥中县| 安阳市|