C#中的數據類型
通用類型系統
C#中,變量是值還是引用僅取決於數據類型
所有的數據類型都是對象。因為它們具有自己ide方法和屬性
int int_value = 101;
//調用*int_value*的比較方法與整型*2*進行進行比較
int_value.CompareTo(2);
//在控制台輸出
Console.WriteLine(int_value.ToString());
值類型
內置值類型
整型
-
sbyte(System.SByte)
-
short(System.Int16)
-
int(System.Int32)
-
long(System.Int64)
-
byte(System.Byte)
-
ushort(System.UInt16)
-
uint(System.UInt32)
-
ulong(System.UInt64)
-
char(System.Char)
浮點型
-
float (System.Single)
-
double(System.Double)
高精度類型
- decimal(System.Decimal)
bool(System.Boolean)
用戶定義的值類型
結構體類型(派生於System.ValueType)
枚舉類型結構體類型(派生於System.Enum)
可空類型結構體類型(派生於System.Nullable
泛型結構體)
C#的所有值類型均隱式派生自System.ValueType;
判斷是否為值類型
Type.IsValueType
引用類型
數組(派生於System.Array)
用戶可以自定義的引用類型
類:class(派生於System.Object)
接口:interface
委托類型:delegate(派生於System.Delegate)
字符串:string(System.String的別名)
Lambda表達式
數組類型
值類型數組
int[] int_array = new int[10];
-
在堆內存中一次初始化10個int類型的存儲空間
-
自動初始化這10個元素
-
將10個元素存儲到剛剛分配的內存空間內
引用類型數組
object[] obj_array = new object[10];
-
在堆內存中分配一次空間
-
不會自動初始化任何元素
obj_array[i]
都是null
-
當有代碼初始化某個元素時,對應元素的存儲空間會分配在堆內存上
obj_arr[i]=new object();
內存部署
堆內存上存儲了所有的引用類型
object item = new objct();
-
new
關鍵字在堆內存中分配內存空間,並且返回該內存空間的地址 -
item
存儲分配后返回的內存地址
語法
結構體
關鍵字struct
定義
public struct LORect
{
private float x;
private float y;
private float width;
private float height;
}
構造函數
public LORect(float x,float y,float width,float height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
- 不允許重載無參構造函數
屬性
public float X{
set{
this.x = value;
}
get{
return this.x;
}
}
定義變量
LORect frame = new LORect(0f,0f,100f,100f);
枚舉
關鍵字enum
定義
public enum LOControlType
{
LOControlTypeNormal = 0,
LOControlTypeHighlight = 1,
LOControlTypeDisable = 2,
}
定義變量
LOControlType type = LOControlType.LOControlTypeNormal;
類
父類
關鍵字class定義
public class LOPerson
{
private string name;
private int age;
}
構造函數
-
public LOPerson(){}
-
public LOPerson(string name){this.name = name;}
-
public LOPerson(string age){this.age = age;}
-
多個參數的構造函數
public LOPerson(string name,int age) { this.name = name; this.age = age; }
自定義函數
public void SayHi()
{
Console.WriteLine (this.name + “: Hello”);
}
析構函數
~LOPerson()
{
this.name = null;
this.age = 0;
}
-
在析構函數中,將引用類型成員變量置為null,內存處理
-
在析構函數中,將值類型成員變量置為默認值,程序邏輯安全
子類
關鍵字class定義
public class LOStudent:LOPerson
{
private float score;
}
構造函數
- 基於父類無參的構造函數
public LOStudent(float score):base()
{
this.score = score;
}
-
public LOStudent(int age):base(age){}
-
在構造函數的繼承中,都會先調用父類的構造函數
自定義函數
public void SayHello()
{
base.SayHi();
Console.WriteLine (“this.SayHi”);
}
- 在自定義函數中,調用父類的某個函數要用到base關鍵字
析構函數
~LOStudent()
{
this.score = 0f;
}
-
子類和父類的析構函數的執行順序
-
1.自動調用子類的析構函數
-
2.自動調用父類的析構函數
-
不需要特別語法指名
-
特性
關鍵字Attribute
定義
[AttributeUsage(AttributeTargets.Property)]
public class LOTypeAttribute:Attribute
{
public string Type{set;get;}
}
特性的用法
public class LOPeople
{
[LOType(Type=“NoHealthy”)]
public string hobby{set;get;}
}
獲取特性的值
PropertyInfo item = property_list[0];
LOTypeAttribute attribute = (LOTypeAttribute)Attribute.GetCustomAttribute(item,typeof(LOTypeAttribute));
Console.WriteLine (attribute.Type);
反射
命名空間
using System.Reflection;
獲取Type
LOPeople people = new LOPeople();
people.hobby = “Smoke”;
Type p_type = people.GetType();
屬性(PropertyInfo)
-
獲取屬性列表
PropertyInfo[] property_list = p_type.GetProperties();
-
獲取指定屬性
PropertyInfo property = p_type.GetProperty(“hobby”);
-
方法
-
獲取people對象的屬性值 .
GetValue()
property.GetValue(people,null);
-
設置people對象的屬性值 .
SetValue()
property.SetValue(people,”Drink”,null);
-
方法(MethodInfo)
-
獲取方法列表
MethodInfo[] method_list = p_type.GetMethods();
-
獲取指定方法
MethodInfo method = p_type.GetMethod(“SayHi”);
-
方法調用
Invoke()
-
方法的參數列表
method.GetParameters();
-
方法的返回值
method.ReturnType;
成員(MemberInfo)
-
獲取成員列表
MemberInfo member_list = p_type.GetMembers();
-
獲取指定成員
MemberInfo[] member= p_type.GetMember(“name”);
構造函數(ConstructorInfo)
-
獲取構造函數列表
ConstructorInfo[] constructor_list = p_type.GetConstructors();
-
獲取指定構造函數
ConstructorInfo constructor = p_type.GetConstructor(new Type[]{typeof(int)});
委托
關鍵字delegate
public delegate void LODataSource(List<int> data_list);
定義委托類型變量
LODataSource dataSource;
委托類型變量賦值
Lambda表達式賦值
dataSource = (List<int> data_list)=>{
foreach (int item in data_list)
{
Console.WriteLine (item.ToString());
}
} ;
函數地址賦值
void ProcessData(List<int> data_list)
{
foreach (int item in data_list)
{
Console.WriteLine (item.ToString());
}
}
-
dataSource = new LODataSource(ProcessData);
-
dataSource = ProcessData;
接口
關鍵字interface
public interface LOInterface{
void SayNice();
}
接口中只能聲明函數,不能實現函數
接口的用法
public class LOTeacher:LOInterface{
private string name;
public void SayNice()
{
Console.WriteLine (this.name + “: Nice”);
}
}
public class LOManager:LOInterface{
private string name;
public void SayNice()
{
Console.WriteLine (this.name + “: Nice”);
}
}