ITPub博客

首页 > 应用开发 > IT综合 > .net 2.0 Nullable泛型结构的应用

.net 2.0 Nullable泛型结构的应用

原创 IT综合 作者:yuzhangqi 时间:2008-12-16 14:25:01 0 删除 编辑

.Net framework 2.0 新增了Nullable泛型结构。它表示基础类型为值类型的对象,值类型与引用类型一样也可以分配空(null)引用。

示例代码:

public class Employee
{
private int _id;
private string _name;
private string? _gender;
private Datetime? _birthday;

public string? Gender
{
get {return _gender;}
set {_gender = value;}
}

public Datetime? Birthday
{
get {return _birthday;}
set {_birthday = value;}
}
}


public EmployeeClient
{
Employee emp = new Employee();

emp.Name = "james";
emp.Gender = null;
emp.Birthday=null;

Emp.Save();
}

在以上示例代码中,Employee的Birthday和Gender属性可接受null值,并将null值持久化到数据库。

Nullable泛型结构为处理值类型的空值提供了便利。

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13651903/viewspace-1014793/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-06-10

  • 博文量
    102
  • 访问量
    763298