当前位置: 首页 > news >正文

网站建设 通讯员银川市建设局网站

网站建设 通讯员,银川市建设局网站,c mvc网站开发实例教程,注册域名费用可空值类型 C#2推出可空类型来表示可以为null的值类型。这是一个呼声很高的需求#xff0c;因为在常用的数据库中都是允许某些值类型可为空的。那么为什么值类型就不能为空呢#xff1f;内存中用一个全0的值来表示null#xff0c;但是全0的地址说明了这个内存空间是被清除了…可空值类型 C#2推出可空类型来表示可以为null的值类型。这是一个呼声很高的需求因为在常用的数据库中都是允许某些值类型可为空的。那么为什么值类型就不能为空呢内存中用一个全0的值来表示null但是全0的地址说明了这个内存空间是被清除了的。所以对象选择用这种方式来初始化。用byte类型来举个例子byte类型用8位来表示一个值也就是说byte类型可以表示的数据最多是256个(2的8次方。这256个值中的每一个值都是有用的我们不可能吧其中一个值永远的舍弃掉从而来表示一个null值。对于引用类型引用类型的变量的值在内存中在特定的操作系统中是固定的32位是4字节64位是8字节32位的地址能最多表示42亿个对象就是说这个能表示的地址太多了所以可以将一个全0的地址单独拿出来作为null值来用因为地址多得永远都用不完。 先来看一下C#1中还没有可空值类型的时候是如何解决这些需求的。 方法1使用一个魔值。确切的来说是使用一个特定值来表示一个空值比如DateTime.MinValue但是这个办法很牵强没有很好的说服力它只是一个实现可控值类型的权宜之计。 方法2引用类型的包装值类型的优点之一是不需要GC去回收把一个值类型包装成一个引用类型GC的开销问题没有很好的解决。同时值类型同引用类型的转换会造成装箱和拆箱虽然C#2的泛型能够解决这个问题但是开头说的垃圾回收没有解决。 方法3用一个全新的值类型来表示可空值类型这个值类型中包含一个bool值来表示是否包含一个真正的值并包含一个表示这个值的属性。方法3正是C#2中做出的最终的决定NullableT,可空值类型。 一、定义 可空类型的核心部分是System. Nullable T。 除此之外静态 类 System. Nullable 提供了一些工具方法 可以简化可空类型的使用。 先来看一下定义 //// 摘要:// Represents a value type that can be assigned null.//// 类型参数:// T:// The underlying value type of the System.Nullable1 generic type.public struct NullableT where T : struct{//// 摘要:// Initializes a new instance of the System.Nullable1 structure to the specified// value.//// 参数:// value:// A value type.public Nullable(T value);//// 摘要:// Gets a value indicating whether the current System.Nullable1 object has a valid// value of its underlying type.//// 返回结果:// true if the current System.Nullable1 object has a value; false if the current// System.Nullable1 object has no value.public bool HasValue { get; }//// 摘要:// Gets the value of the current System.Nullable1 object if it has been assigned// a valid underlying value.//// 返回结果:// The value of the current System.Nullable1 object if the System.Nullable1.HasValue// property is true. An exception is thrown if the System.Nullable1.HasValue property// is false.//// 异常:// T:System.InvalidOperationException:// The System.Nullable1.HasValue property is false.public T Value { get; }//public override bool Equals(object other);//// 摘要:// Retrieves the hash code of the object returned by the System.Nullable1.Value// property.//// 返回结果:// The hash code of the object returned by the System.Nullable1.Value property// if the System.Nullable1.HasValue property is true, or zero if the System.Nullable1.HasValue// property is false.public override int GetHashCode(); // // 摘要: // Retrieves the value of the current System.Nullable1 object, or the objects // default value. // // 返回结果: // The value of the System.Nullable1.Value property if the System.Nullable1.HasValue // property is true; otherwise, the default value of the current System.Nullable1 // object. The type of the default value is the type argument of the current System.Nullable1 // object, and the value of the default value consists solely of binary zeroes. public T GetValueOrDefault(); // // 摘要: // Retrieves the value of the current System.Nullable1 object, or the specified // default value. // // 参数: // defaultValue: // A value to return if the System.Nullable1.HasValue property is false. // // 返回结果: // The value of the System.Nullable1.Value property if the System.Nullable1.HasValue // property is true; otherwise, the defaultValue parameter. public T GetValueOrDefault(T defaultValue); // // 摘要: // Returns the text representation of the value of the current System.Nullable1 // object. // // 返回结果: // The text representation of the value of the current System.Nullable1 object // if the System.Nullable1.HasValue property is true, or an empty string () if // the System.Nullable1.HasValue property is false. public override string ToString(); public static implicit operator T? (T value); public static explicit operator T(T? value); } 包含一个接受一个参数的构造函数这个构造函数传入一个非可空值进行初始化例如Nullableint anew Nullableint(5);包含一个HasValue属性来指示是否包含一个真正的值。如果HasValue返回true可以使用Value属性来返回这个值。如果没有判断HasValue直接使用Value可能会得到一个System.InvalidOperationException的异常。Nullable是不易变的一般来说值类型都是不易变的也就是说已经创建就不会再改变。值类型的复制方式就是复制一个副本这个副本会在内存形成一个新的地址。GetValueOrDefault()可以返回一个值如果有或者返回一个null如果没有它有重载的方法向内部传入一个字面量会在没有真正的值存在的情况下拿这个传入的字面量当作一个默认值。其他的方法都是重写了object里面的。最后 框架提供了两个转换。 首先 是T到Nullable T 的隐式转换。 转换结果为一个HasValue属性为true的实例。 同样NullableT 可以显式转换为T 其作用与Value属性相同 在没有真正的值可供返回时将抛出一个异常。 包装 wrapping 和 拆 包 unwrapping--- 将T的实例转换成NullableT 的实例的过程在C#语言规范中称为包装 相反的过程则称为拆包。 在C#语言规范中 分别是在涉及“ 接受一个参数的构造函数” 和 Value 属性时定义的这两个术语。 二、NullableT的装箱和拆箱 它是值类型所以会涉及到装箱和拆箱。 装箱NullableT的实例要么装箱成空引用如果没有值要么装箱成T的一个已装箱值。不存在“装箱的可空int”。 Nullableint nullable 5;object boxed nullable;Console.WriteLine(boxed.GetType());//这里会输出System.Int32印证了不存在“装箱的可空int”。   拆箱已装箱的值要么拆箱成一个普通类型要不拆箱成对应的可空类型。拆箱一个空引用时如果拆箱成普通类型会抛出System.NullReferenceException Nullableint a new Nullableint();object b a;int c (int) b;//System.NullReferenceException 但如果拆箱成恰当的可空值类型则会产生一个没有值的实例。下面的则不会抛出错误 Nullableint a new Nullableint();object b a;var c (Nullableint) b; 三、NullableT实例的相等性 Nullable T 覆盖了object.Equals( object)注意这个方法需要传入一个object的类型的实例所以传入的Nullable会进行装箱。Nullable装箱的原理上面有描述。这个方法基本与我们期望的一致没有什么大的问题。 四、来自非泛型Nullable类的支持 Nullable有两个方法 public static int CompareT(T? n1, T? n2) where T : struct;public static bool EqualsT(T? n1, T? n2) where T : struct; Compare是用CompareT.Default来比较两个的大小 Nullableint a 5;Nullableint b 5; var defaults Comparerint?.Default;Console.WriteLine(defaults.Compare(a,b));//0 Equals使用EqualityComparer T. Default。 对于没有值的实例 上述每个方法返回的值都遵从.NET的约定空值与空值相等 小于其他所有值。   五、语法糖 。使用这个修饰符放到int后面int的含义与Nullableint的含义完全一样包括生成的IL。就像int和System.Int32换着用一样。 与null值的比较 可以将null赋给一个可空值类型的实例表示一个没有真实值的可空值类型。 int? a null; 可空值类型和其基础类型的一些匹配的行为 标题中提到的基础类型是这么一个概念int?的基础类型就是int。 假如一个非可空的值类型支持一个操作符或者一种转换而且那个操作符或者转换只涉及其他非可空的值类型时 那么可空的值类型也支持相同的操作符或转换。 下面举一个更具体的例子。 我们知道int到long存在着一个隐式转换 这就意味着 int? 到 long? 也存在一个隐式转换 其行为可想而知。 因为int和long存在这么一种隐式转换的规则那么就存在 int?到long的隐式转换 int到long?的隐式转换 int?到long的显示转换。 int a 1; long? b a;//隐式转换 int? c 1; long? d c;//隐式转换 int? e 1; long f (long) e;//显示的转换   转换和操作符 将基础类型的一些转换行为应用到对应的可空类型上的转换行为叫做提升转换将基础类型的一些操作符的行为应用到相应可空值类型的操作符叫做提升操作符。基础类型重载操作符后对应的可空类型就可享用这个重载。 这里的只是还需要从书中进行补充未完待续。。。。 空合并操作符 这个二元操作符在对 first ?? second 求值 时 大致会经历以下步骤 1、对first进行求值 2、如果结果非空表达式的值和类型就已经确定 3、如果结果为空继续计算second的值并将结果的值作为整个表达式的值。 int? a null;int c a ?? 1; 很明显这里涉及到了类型转换的问题我们可以直接将int类型的c变量赋给这个表达式因为second是非可空的。转载于:https://www.cnblogs.com/pangjianxin/p/8652016.html
http://www.zqtcl.cn/news/52411/

相关文章:

  • 网站建设电苏州网站开发公司兴田德润在那里
  • 黄石专业网站建设推广做网站还能赚钱吗
  • 中国建设银行官网站账户商品专业的手机网站建设公司哪家好
  • 代发货网站建设装修行业网站建设
  • 服务器屏蔽网站wordpress 评论 图片不显示
  • 企业网站的种类司法局网站建设方案
  • 网站经营性备案多少钱学做网站能赚多少
  • 怎么用VS2012建设网站少主网络建站
  • 巫山做网站那家好单页网站
  • 电子商务网站建设的方法和工具做网站哪里比较好
  • 阿里巴巴上做网站润商网站建设服务
  • 工业设计考研比较好的学校枣庄做网站优化
  • 公司的网站建设jnzji如何自建商城和电商平台
  • 设计网站无锡网络推广服务合同模板
  • wordpress建站 知乎asp网站开发 pdf
  • 外贸企业网站开发企业一号wordpress主题
  • 经营性 网站备案常州网站外包
  • 海东地网站建设网站的导航栏怎么做
  • 优质做网站公司怎样做instergram网站营销
  • 网站开发的作用容桂网站制作公司
  • 手机直播网站开发官方网站建设需要做哪些东西
  • 搭建网站上传文件98元建网站
  • 南京外贸网站建设案例建设工程造价管理协会网站
  • 百度贴吧网站开发需求分析咨询公司的经营范围有哪些
  • 云南做企业建站可以在什么网站做二建题目
  • 域名购买后网站搭建wp如何转换wordpress
  • 旅游商业网站策划书丽水百度seo
  • 设计logo网站生成器前端好学还是后端好学
  • 山东省建设协会网站专业做财经直播网站有哪些
  • 重庆网站优化公司哪家便宜网站设计需求文档