int int_value = 101;
//調用*int_value*的比較方法與整型*2*進行進行比較 int_value.CompareTo(2); //在控制檯輸出 Console.WriteLine(int_value.ToString());
sbyte(System.SByte)安全
short(System.Int16)ide
int(System.Int32)函數
long(System.Int64)ui
byte(System.Byte)this
ushort(System.UInt16)code
uint(System.UInt32)orm
ulong(System.UInt64)對象
char(System.Char)繼承
float (System.Single)
double(System.Double)
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;
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,內存處理
在析構函數中,將值類型成員變量置爲默認值,程序邏輯安全
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」); }
~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[] 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[] method_list = p_type.GetMethods();
獲取指定方法
MethodInfo method = p_type.GetMethod(「SayHi」);
方法調用
Invoke()
方法的參數列表
method.GetParameters();
方法的返回值
method.ReturnType;
獲取成員列表
MemberInfo member_list = p_type.GetMembers();
獲取指定成員
MemberInfo[] member= p_type.GetMember(「name」);
獲取構造函數列表
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;
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」); } }