1. 程式人生 > >C#學習筆記整理1--資料型別

C#學習筆記整理1--資料型別

什麼是資料型別(Data Type)

A data type is a homogeneous collection of values, effectively presented, equiipped with a set of operations which manipulate these values

C#型別中所包含的資訊

  1. 儲存此型別變數所需要的記憶體空間
  2. 此型別的值可表示的最大,最小值的範圍
  3. 此型別所包含的成員(如方法,屬性,事件等)
  4. 此型別由何基類派生而來
  5. 程式執行的時候,此型別的變數應該分配在記憶體的什麼位置 程式執行之後,在記憶體中包括兩個部分: a)棧–Stack 給函式呼叫使用 Stack Overflow棧溢位(無限遞迴,手動從棧裡切記憶體過多都會導致) b)堆–Heap 記憶體洩漏(宣告的物件,使用完畢後,沒有回收,導致資源浪費)
  6. 此型別所允許的操作

C#的五大資料型別

引用型別
  1. 類(class):如Windows,Form ----------------class
  2. 介面--------------------------------------------interface
  3. 委託--------------------------------------------delegate
值型別
  1. 結構(Structures):如Int32,Int64 ---------struct
  2. 列舉--------------------------------------enum
所有型別的基類object
using
System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace StudyTest { public class DataType { public static void ShowInt32() { Type type = typeof(int); Console.
WriteLine($"int型別的類名為:{type.Name}"); Console.WriteLine($"int型別的父類的類名為:{type.BaseType.Name}"); //獲取int型別的方法簽名 MethodInfo[] intMethods= type.GetMethods(); Console.WriteLine($"方法名稱\t訪問修飾符\t返回值型別\t引數列表"); foreach (MethodInfo method in intMethods) { StringBuilder stringBuilder = new StringBuilder(); //輸出方法名 stringBuilder.Append(method.Name+ "\t"); //輸出方法的訪問修飾符 stringBuilder.Append((method.IsPublic?"public":method.IsPrivate?"private":"")+"\t"); //輸出方法的返回值型別 stringBuilder.Append(method.ReturnType.Name == null ? "void" : method.ReturnType.Name+"\t"); //獲取每個方法的引數列表 ParameterInfo[] parameterInfos = method.GetParameters(); foreach (ParameterInfo p in parameterInfos) { //獲取每個引數的型別和名稱 stringBuilder.Append($"({p.GetType().Name} {p.Name})"); } //輸出結果 Console.WriteLine(stringBuilder.ToString()); } } } }