Tag:

C#性能增益从SqlDataReader返回Nullable类型

我有一个简单的方法,从DataReader而不是内置的GetInt32返回Nullable Int32。 我多次称这种方法有一种情况,任何时候我都可以剃掉它会有益。 任何人都可以建议任何替代和更快的方法从DataReader中获取可以为空的Int32吗? private Int32? GetNullableInt32(SqlDataReader dataReader, int fieldIndex) { return !dataReader.IsDBNull(fieldIndex) ? dataReader.GetInt32(fieldIndex) : (Int32?)null; }

变量类型以?结尾?

什么 意思: public bool? Verbose { get; set; } 当应用于string? ,有一个错误: 类型’string’必须是非可空值类型才能在generics类型或方法’System.Nullable’中将其用作参数’T’

具有Nullable值的SqlParameter在ExecuteNonQuery时给出错误?

我有一个SQL查询,其参数在数据库(Sql Server)中可以为null。 更新方法正常工作,直到该用户在字段中放置一个空白,这将为DataTime对象生成一个空值(此对象可以为空)。 问题是当dbCommand.ExecuteNonQuery(); 。 以下是我为此字段构建参数的方法: IDataParameter dbParam_au_id = new SqlParameter(); dbParam_au_id.ParameterName = “@birthday”; dbParam_au_id.Value = birthday; dbParam_au_id.DbType = DbType.DateTime; dbCommand.Parameters.Add(dbParam_au_id); 我尝试将生日的null值转换为DBNull.Value,如下所示: IDataParameter dbParam_au_id = new SqlParameter(); dbParam_au_id.ParameterName = “@birthday”; dbParam_au_id.Value = birthday??DBNull.Value; dbParam_au_id.DbType = DbType.DateTime; dbCommand.Parameters.Add(dbParam_au_id); 但是这段代码不能编译,我得到错误: 错误1运算符’??’ 不能应用于’System.DateTime?’类型的操作数 和’System.DBNull’ 任何的想法?

通过reflection检测可空类型

令人惊讶的是,以下代码未通过Assert: int? wtf = 0; Assert.IsType<Nullable>(wtf); 所以只是出于好奇,你如何确定给定的实例是否是Nullable 对象?

GetValueOrDefault如何工作?

我负责一个LINQ提供程序,它对C#代码执行一些运行时评估。 举个例子: int? thing = null; accessor.Product.Where(p => p.anInt == thing.GetValueOrDefault(-1)) 目前上面的代码不能与我的LINQ提供程序一起使用,因为thing为null。 虽然我已经使用C#很长时间了,但我不知道如何实现GetValueOrDefault,因此我应该如何解决这个问题。 所以我的问题是: GetValueOrDefault如何在调用它的实例为空的情况下工作? 为什么不抛出NullReferenceException ? 接下来的问题:我应该如何使用reflection来复制对GetValueOrDefault的调用,因为我需要处理空值。

AND操作不能在可空的bool之间应用

我在两个可以为空的布尔值( bool? )之间应用AND操作( && ),但它给了我错误 运算符&&不能应用于bool?类型的操作数bool? 和bool? 我如何在包含两个可空的bool的声明中应用和操作? 如果我有像这样的对话结果 dialog.ShowDialog () == DialogResult.OK 我怎样才能将它转换为可以为空的bool,因为我需要在其他操作数返回可以为空的bool的if条件中放入’&&’运算符? 这是代码: if (dialog.ShowDialog () == DialogResult.OK && CheckProjectPath(dialog.FileName, true)) 这个if条件中的第二个操作数是可以为空的bool。

generics类型参数和Nullable方法重载

你好 我有使用generics和可空的代码: // The first one is for class public static TResult With(this TInput o, Func evaluator) where TResult : class where TInput : class // The second one is for struct (Nullable) public static TResult With(this Nullable o, Func evaluator) where TResult : class where TInput : struct 请注意TInput约束,一个是类,另一个是struct。 然后我用它们: string s; int? i; […]

为什么私有变量定义上有问号?

我正在阅读一篇关于MVVP模式的文章以及如何使用WPF实现它。 在源代码中有多行,我无法弄清楚它中的问号代表什么。 private DateTime? _value; 这是什么? 在定义中意味着什么? 我试图在VS的帮助下找到它,但失败了。

从Nullable类型获取reflection中的PropertyType.Name

我想使用reflection获取属性类型。 这是我的代码 var properties = type.GetProperties(); foreach (var propertyInfo in properties) { model.ModelProperties.Add( new KeyValuePair (propertyInfo.PropertyType.Name, propertyInfo.Name) ); } 这段代码propertyInfo.PropertyType.Name ,但是如果我的属性类型是Nullable我得到这个Nullable’1字符串,如果得到这个名字,可以写FullName System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

C#空传播 – 魔法在哪里发生?

空传播是一个非常好的function – 但实际的魔法在哪里以及如何发生? frm?.Close()在哪里frm?.Close()变为if(frm != null) frm.Close(); – 它实际上是否真的改变了那种代码?