C#學習筆記

來源:互聯網
上載者:User

標籤:

1、C#程式結構

  C#是利用命名空間組織起來的,using將命名空間名所標識的命名空間內的類型成員匯入當前編譯單元中,從而可直接使用每個被匯入的類型的標識符。

  Main方法是程式的進入點,C#中所有的Main方法都必須是靜態。

static void Main(){}

2、變數及類型

  裝箱:將實值型別轉換為參考型別;拆箱:將參考型別轉換為實值型別。

  定義一個局部變數時,一定要對其初始化;儘可能少定義全域變數。

  布爾類型變數的值只能是true或false,不能與其他類型進行轉換。

  Char在C#中表示一個Unicode字元。

  string是String的別名,表示Unicode字元的字串;產生和構建一個長的字串時,一定使用StringBuilder類型,而不用string類型。String對象是不可改變的,每次使用String類中的方法時都會在記憶體中建立一個新的字串對象,這就需為該對象分配新的空間。若要修改字串而不建立新的對象,則可使用StringBuilder類。

  集合類型:Array、ArrayList、Hashtable

3、屬性和方法

  C#屬性有兩種:在公用語言運行庫中定義的屬性(特性)、自訂屬性(get和set訪問器),如下所示:

namespace CSharpTest{    [DefaultPropertyAttribute("StationName")]    class Station    {        private string _StationName;        private double _Lon = 103;        private double _Lat = 38;        private Color _color;        private string _file = string.Empty;        private Font _font;        [CategoryAttribute("常規"), DescriptionAttribute("檔案名稱"), ReadOnlyAttribute(true)]        public string FileName        {            get { return _file; }            set { _file = value; }        }        [CategoryAttribute("顯示"), DescriptionAttribute("顏色"), DisplayNameAttribute("顏色")]        public Color Color        {            get { return _color; }            set { _color = value; }        }        [CategoryAttribute("顯示"), DescriptionAttribute("字型")]        public Font Font        {            get { return _font; }            set { _font = value; }        }        public string StationName        {            get { return _StationName; }            set { _StationName = value; }        }        public double Lon        {            get { return _Lon; }            set { _Lon = value; }        }        public double Lat        {            get { return _Lat; }            set { _Lat = value; }        }      }}
View Code

  get訪問器和方法體相似,須返回屬性類型的值;而set訪問器類似於傳回型別為void的方法,它使用稱為value的隱式參數。

  類的執行個體化對象不能調用靜態方法,必須直接使用類名調用。

4、結構、類和介面

  結構的執行個體化可以不使用new運算子;結構可以聲明建構函式,但它們須帶參數;在結構聲明中,除非欄位被聲明為const或static,否則無法初始化。

  在沒有對類執行個體化前,無法用類名調用類中的方法或欄位。

  常用類修飾符:new、private、protected、public、internal(只有其所在的類才能訪問)、abstract(抽象類別)、sealed(密封類,不允許被繼承)。 

  C#類特性:封裝、繼承(只支援單繼承)、多態。

  介面用interface聲明,通過類繼承來實現。通過介面可實現多重繼承,一個類雖只能繼承一個基類,但可繼承任意介面。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Test1{    //聲明三個介面    interface IPeople    {        string Name        {            get;            set;        }        string Sex        {            get;            set;        }    }    interface ITeacher : IPeople    {        void teach();    }    interface IStudent : IPeople    {        void study();    }    //類繼承自多個介面,並實現介面    class Program:IPeople,ITeacher,IStudent     {        string name = "";        string sex = "";        public string Name        {            get            {                return name;            }            set            {                name = value;            }        }        public string Sex        {            get            {                return sex;            }            set            {                sex = value;            }        }        public void teach()        {            Console.WriteLine(Name + " " + Sex +" 教師");        }        public void study()        {                    Console.WriteLine(Name + " " + Sex +" 學生");        }        static void Main(string[] args)        {            Program program = new Program();//執行個體化對象            ITeacher iteacher = program;            iteacher.Name = "Tea";            iteacher.Sex = "男";            iteacher.teach();            IStudent istudent = program;            istudent.Name = "Stu";            istudent.Sex = "女";            istudent.study();            Console.ReadKey();        }    }}
View Code

  抽象類別與介面的:

  • 抽象類別與介面都不能直接執行個體化,但可聲明它們的變數;
  • 抽象類別中可定義成員的實現,但介面不可以;
  • 抽象類別可包含欄位、構造和解構函式、靜態成員或常量等,但介面中不可以;介面可包含事件、索引器、方法和屬性;
  • 介面中的成員必須是公用的;

  聲明密封方法時,sealed修飾符總是和override修飾符同時使用。

  使用partial定義部分類別,部分類別的聲明須與其他部分位於同一命名空間。

5、異常處理

  try...catch...finally語句

6、迭代器與泛型

  迭代器(iterator)有時又稱遊標(cursor),是可返回相同類型的值的有序序列的一段代碼,是程式設計的軟體設計模式(Design Pattern),可在容器上遍曆介面,設計人員無需關心容器的內容。

  C#迭代器代碼使用yield return語句依次返回每個元素,yield break語句終止迭代;其傳回型別須為IEnumerable或IEnumerator中的任意一種。建立迭代器最常用的方法是對IEnumerator介面實現GetEnumerator方法。

  泛型是用於處理演算法、資料結構的一種編程方法,主要是為了提高代碼的重用性。泛型的使用如下所示:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Finder{    public class Finder    {        public static int Find<T>(T[] items, T item)        {            for (int i = 0; i < items.Length; i++)            {                if (items[i].Equals(item))                {                    return i;                }            }            return -1;        }    }    class Program    {        static void Main(string[] args)        {            int i = Finder.Find<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 6);            Console.WriteLine("6 在數組中的位置:" + i.ToString());            Console.ReadKey();        }    }}
View Code

7、IO操作

  檔案、檔案夾、XML、ini設定檔、註冊表、資料庫

8、網路編程

  Sockets、Mail、Web

9、線程

10、表單

  Form表單、MDI表單、各種控制項(ListBox、TreeView、DockPanel、水晶報表、列印)、GDI+圖形映像技術

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.