Tag: null

System.NullReferenceException检查是否!= null

我正在使用ASHX处理程序,我希望处理程序检查Session!= null。 if (context.Session[“Username”] != null) 我得到这个错误指向这一行: System.NullReferenceException:未将对象引用设置为对象的实例。 有什么问题?

是否有方便的语法来检查可以为null的嵌套属性?

可能重复: Deep Null检查,有更好的方法吗? 例如,如果您在Foo1.Bar1.Foo2.Bar2上执行逻辑(并且每个属性都可以为null),那么您不能只对foo.Bar1.Foo2.Bar2执行此操作,因为您可能会得到null引用exception 目前这就是我的工作 if (foo1!=null && foo1.Bar1!=null && foo1.Bar1.Foo2 !=null && foo1.Bar1.Foo2.Bar2!=null) return DoStuff(foo1.Bar1.Foo2.Bar2); //actually a logic based on the value of Bar2 else return null; 这样做有更优雅或方便的方式吗?

什么时候generics参数永远不会为空

在通用的GetHashCode(T foo)方法中,我检查foo是否为null 。 然而,我偶然发现了一个奇怪的Resharper警告。 在下面的代码中, foo永远不会为null吗? private class FooComparer : IEqualityComparer where T: Foo { public int GetHashCode(T foo) { // resharper warning: “Expression is always false” if (Object.ReferenceEquals(null,foo)) return 0; // … calculate hash } } 但据我所知,以下是完全合法的: Foo foo = null; var fooComparer = new FooComparer(); int hash = fooComparer.GetHashCode(foo);

System.Nullable null * int值的值是多少?

请考虑以下声明: int? v1 = null; int? v2 = 5 * v1; v2的价值是多少? ( null或空字符串?) 如何防止编译器将其标记为无效操作? 我是否需要遵循自定义exception处理?

将null与对象而不是具有null的对象进行比较有什么问题

我刚刚发现我可以将null与这样的Object进行比较, if(null != Object) 而不是将Object与null进行比较,例如 Object != null 如果使用前一种方法可能会出错? 这合法吗? 如果没有,那么为什么编译器接受它?

.Net C#String.Join如果元素值为null,如何输出“null”而不是空字符串?

根据String.Join的MSDN文档 “如果value中的任何元素为null,则使用空字符串。” 代码我从DataTable中提取数据 rotationValues = string.Join(“, “, from r in rotationData.Rows.OfType() select r[5]); 这将导致输出类似于: 8, 7, , 12, , , 13, 有没有办法简单地让它“null”代替空字符串,如下所示: 8, 7, null, 12, null, null, 13, null

如何安全地将可空结果从sqlreader转换为int?

我有一个包含空值的表,我需要使用SqlDataReader从表中获取数据。 我无法弄清楚如何安全地将DBNull转换为int。 我现在正以这种方式做到这一点: … reader = command.ExecuteReader(); while (reader.Read()) { int y = (reader[“PublicationYear”] != null) ? Convert.ToInt32(reader[“PublicationYear”]) : 0; … } … 但是获取Object cannot be cast from DBNull to other types. 当PublicationYear为null时。 我怎样才能安全地获得价值? 谢谢。

对IComparable对象进行排序,其中一些对象为null

大多数人在编写实现IComparable 的refence类型(类)时,使用null比任何实际对象少的约定。 但是如果你尝试使用相反的约定,会发生一些有趣的事情: using System; using System.Collections.Generic; namespace SortingNulls { internal class Child : IComparable { public int Age; public string Name; public int CompareTo(Child other) { if (other == null) return -1; // what’s your problem? return this.Age.CompareTo(other.Age); } public override string ToString() { return string.Format(“{0} ({1} years)”, this.Name, this.Age); } } internal static […]

“null this”是否可以使用扩展方法?

所以,我真的很喜欢使用扩展方法..也许有点太多了。 所以,我会问我最近的享受,以确保我不会走得太远。 情景是我们有一个Guid? 传入的变量。如果变量为null或Guid.Empty ,那么我们想要使用不同的Guid。 所以,我写了一个扩展方法,让它像英文一样读: internal static Guid OrIfEmpty(this Guid? guid, Guid other) { if (!guid.HasValue || guid.Value == Guid.Empty) { return other; } return guid.Value; } 这自动意味着“null this”不会抛出exception。 例如,这将工作: ((Guid?)null).OrIfEmpty(other); 如果不使用扩展方法,这是不可能的,在我看来可能会产生误导。 然而,它只是如此简洁和干净! 所以你怎么看? 这是可以接受的事情,还是对其他程序员来说太混乱了? 此外,我确信会有其他情况我会做这样的事情并将其检查为null,但这是我现在最好的例子。 注意:我真的不是在问这个Guid? 特别是生意。 我要求更多关于实现的整体模式(有一个扩展方法,其中可以为null )

如何在linq中处理空值?

recordsList.ListOfRecords = new StudentRecordsBAL() .GetStudentsList() .Select(q => new StudentRecords() { _RollNumber = q._RollNumber, _Class = q._Class, _Name = q._Name, _Address = q._Address, _City = q._City, _State = q._State, _Subjects = q._Subject, _AttendedDays = new AttendanceBAL() .GetAttendanceListOf(q._RollNumber) .Where(date => date != null) .Select(date => new DateTime(date._Date.Year, date._Date.Month, date._Date.Day)) .Distinct() .ToList(), _AttendedSubjects = GetAttendedSubjects(q._RollNumber) }).ToList(); 上述代码中的方法GetAttendanceListOf(q._RollNumber)将返回数据库中的记录列表,如果传递的“roll-no”没有记录,则返回“null”。 linq查询将终止生成错误 […]