Tag: null

将null转换为对象?

我今天遇到了这个代码 AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); 这有什么不对吗?

数据是空的。 在C#.net中连接两个表后,无法在空值上调用此方法或属性

在我的代码中加入表后,出现错误“数据为空。此方法或属性无法在空值上调用”。 请查看我的代码,请帮忙。 我试图在谷歌寻找答案,但仍然没有得到我想做的。 这是导致上述错误的代码: private static string m_sConnectionString = ConfigurationManager.ConnectionStrings[“NomsConnection”].ConnectionString; private static string m_sReport = “SELECT r.[RequestID],r.[RequestDate],r.[PARNumber],r.[StatusID],r.[PurchaseComment]” // 0 – 4 + “,r.[UID],r.[MyUID],r.[FullName],r.[Email]” // 5 – 8 + “,r.[EntityName],r.[DepartmentName],r.[DepartmentID]” // 9 – 11 + “,r.[LastBy]” // 12 + “,r.[ProgramID],r.[ProgramCode],r.[ProgramName],r.[CostCenterCode]” // 13 – 16 + “,p.[PartDesc],p.[SupplierID],p.[AccountType],p.[CurrName],p.[PartQuantity],p.[PiecePrice]” + “FROM [NOP_PR].[dbo].[Requests] r ” + “JOIN [NOP_PR].[dbo].[Parts] p on […]

C#:端点为空(无穷大)时的范围交集

好的,我有这些交集方法来处理范围,只要范围端点不为null,它们就可以正常工作: public static bool Intersects(this Range first, Range second, IComparer comparer) { return comparer.Compare(first.Start, second.End) = 0; } public static Range GetIntersectionWith(this Range first, Range second, IComparer comparer) { // Return null, if any range is null or if they don’t intersect at all if (first == null || second == null || !Intersects(first, second, […]

是否有理由在c#中使用clausule在多个内部检查null?

有没有理由在最后一次使用中检查null? 在我看来,它很可能不会被需要? using (var connection = new SqlConnection(connectionString)) { using (var command = new SqlCommand(commandString, connection)) { using (var reader = command.ExecuteReader()) { if (reader != null) { // Use the reader } } } } 编辑: 我应该关闭reader.Close()和connection.Close()在使用中如果我像这样使用它: using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetailsDZP)) using (var sqlWrite = new SqlCommand(preparedCommand, varConnection)) { while (sqlWrite.Read()) { //something […]

C#ExecuteQuery null值

我有一些代码: using (OAZISDBDataContext ctx = new OAZISDBDataContext()) { IEnumerable details = ctx.ExecuteQuery(“exec [dbo].[zna_contact] {0}, {1}”, “test”, “19601023”, } 但是,我还希望能够将空值传递给存储过程,因此它不会使用它们。 现在使用字符串这很容易,我可以传递String.Empty,它会工作。 但是,如果我想传递空日期,这是一个问题。 我显然试过了: using (OAZISDBDataContext ctx = new OAZISDBDataContext()) { IEnumerable details = ctx.ExecuteQuery(“exec [dbo].[zna_contact] {0}, {1}”, “test”, null, } 但这不起作用,给出错误: System.Exception:queryparameter不能是System.Object类型。 经过一些阅读后,我发现ExecuteCommand不支持null参数,而规范断言它应该。 有没有人遇到过这个问题并找到了解决方法? 谢谢你们

在Parallel.For内部调用的方法中,HttpContext为null

尝试了很多后发布这个问题。 做正常不是一种选择,因为我们需要在很短的时间内完成大量的处理。 我有GetDataFor() ,其中使用了HttpContext.Current 。 代码如下所示: public void SomeMethod() { var context = HttpContext.Current; Parallel.For(0, 100, i => { var data = GetDataFor(i, context); }); } public data GetDataFor(int i, HttpContext context) { Uri requestUri = null; if (HttpContext.Current != null) { requestUri = HttpContext.Current.Request.Url; sCookie = string.Format(“{0}”, HttpContext.Current.Request.Headers[“cookie”]); } else { requestUri = context.Request.Url; […]

设计选择:我是否希望扩展方法在null上抛出exception?

可能重复: C#:在扩展方法中validation“this”参数的最佳实践 我对设计选择感到矛盾,并希望听到SO社区的意见。 我在这里提出的例子只是一个可能的情况,必须进行这种设计选择 – 实际上,可能会有更多的情况。 对这个具体案例和更一般的方法都欢迎回答,并且对于如何在特定案例中做出决定的指导方针也表示赞赏。 基本上,我想知道如何考虑这个问题: 当编写一个本质上没有失败的扩展方法时,如果将null引用作为this实例传递,是否应该对参数执行null检查? 例: 我正在IEnumerable上编写一个扩展方法,它将遍历集合并执行一些Action – 基本上,这就是它的作用: public static void Each(this IEnumerable collection, Action action) { foreach (var t in collection) { action.Invoke(t); } } 我无法决定的是,如果将null传递给任一参数,这个扩展方法应该做什么。 如果我不添加任何空检查,我将在action.Invoke(T)上获得NullReferenceException ,但是如果collection为null ,for循环将默默地执行任何操作(即使action也为null ,也不会抛出任何exception。 )。 我决定添加一个null检查action ,所以我可以抛出ArgumentNullException而不是NullReferenceException 。 但是我想对这个collection做些什么呢? 选项1:添加空检查,并抛出ArgumentNullException 。 选项2:只是默默地让方法什么都不做。 哪个在将来我想使用该方法会更有用? 为什么?

C#Nullable Equality Operations,为什么null <= null解析为false?

为什么在.NET中呢? null >= null 解析为假,但是 null == null 解析为真? 换句话说,为什么不是null >= null等效于null > null || null == null null > null || null == null ? 有人有正式答案吗?

可空类型和ReSharper警告

我有以下代码: private static LogLevel? _logLevel = null; public static LogLevel LogLevel { get { if (!_logLevel.HasValue) { _logLevel = readLogLevelFromFile(); } return _logLevel.Value; } } private static LogLevel readLogLevelFromFile() { … } 我在return语句中收到一个关于可能的System.InvalidOperationException的ReSharper警告,它建议我检查_logLevel以查看它是否为null 。 但是, readLogLevelFromFile返回LogLevel ,而不是LogLevel? ,所以当_logLevel为null时,无法达到return语句。 这只是ReSharper的疏忽,还是我错过了什么?

DateTime可以为null吗?

可能重复: DateTime“null”值 是否可以将datetime对象设置为null?