C#数据类型汇总
2016-03-03 22:14
435 查看
通用类型系统
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”); } }
接口的好处
为多个不同的类提供相同名称的方法接口,提供不同的实现过程
相关文章推荐
- C#数据类型汇总
- C# 通过this关键字来扩展方法
- c# 最简单的委托
- C# 开放类型与封闭类型
- C#成魔之路<1>Windows 应用程序高级控件(1)
- 理解C# 中委托的含义
- c、c#---计时器
- C#开发ftp客户端遇到550错误
- C#成魔之路<开篇>
- 《C#高级编程》读书笔记
- C# WinForm制作电子琴键盘
- C#开发之问题汇总-项目移动路径后编译或者发布错误
- 在C#中将String转换成Enum:
- C#.NET扩展方法
- C# Socket编程 同步以及异步通信
- 在C#中主线程和子线程如何实现互相传递数据
- C# 读取Log4net 日志文件
- C#日期格式转换大全
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
- C#开发之问题汇总-NetFramework的版本选择