nullable

Conversion between Nullable types

梦想与她 提交于 2019-12-19 06:21:12
问题 Is there a converter in .NET 4.0 that supports conversions between nullable types to shorten instructions like: bool? nullableBool = GetSomething(); byte? nbyte = nullableBool.HasValue ? (byte?)Convert.ToByte(nullableBool.Value) : null; 回答1: Not that I am aware of. You could just write a helper method like this: public Nullable<TTarget> NullableConvert<TSource, TTarget>( Nullable<TSource> source, Func<TSource, TTarget> converter) where TTarget: struct where TSource: struct { return source

Nullable DateTime?

旧城冷巷雨未停 提交于 2019-12-19 05:31:11
问题 how to create setter and getter Properties for nullable datetime. for example: private DateTime mTimeStamp; public DateTime TimeStamp { get { return mTimeStamp; } set { mTimeStamp = value; } } Does nullable attributes support setter and getter or have i to declare it public? private DateTime? mTimeStamp; public DateTime TimeStamp { } 回答1: You can just do this instead: public DateTime? TimeStamp { get; set; } If you were having trouble with the compiler it's probably because you only changed

Set property Nullable<> by reflection

混江龙づ霸主 提交于 2019-12-19 05:31:05
问题 I try to set a Nullable<> property dynamicly. I Get my property ex : PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int I want to set my property by reflection like property.SetValue(class,"1256",null); It's not working when my property is a Nullable<> Generic. So i try to find a way to set my property. To know the type of my nullable<> property i execute Nullable.GetUnderlyingType(property.PropertyType)

C# Performance gain returning a Nullable Type from a SqlDataReader

别等时光非礼了梦想. 提交于 2019-12-19 02:27:10
问题 I have a simple method that returns a Nullable Int32 from a DataReader rather than the built in GetInt32. I am calling this method many many times and have one situation where any time I can shave off of it would be beneficial. Can anyone suggest any alternative and faster ways of getting a nullable Int32 out of the DataReader? private Int32? GetNullableInt32(SqlDataReader dataReader, int fieldIndex) { return !dataReader.IsDBNull(fieldIndex) ? dataReader.GetInt32(fieldIndex) : (Int32?)null; }

C# Performance gain returning a Nullable Type from a SqlDataReader

筅森魡賤 提交于 2019-12-19 02:27:04
问题 I have a simple method that returns a Nullable Int32 from a DataReader rather than the built in GetInt32. I am calling this method many many times and have one situation where any time I can shave off of it would be beneficial. Can anyone suggest any alternative and faster ways of getting a nullable Int32 out of the DataReader? private Int32? GetNullableInt32(SqlDataReader dataReader, int fieldIndex) { return !dataReader.IsDBNull(fieldIndex) ? dataReader.GetInt32(fieldIndex) : (Int32?)null; }

Wrong compiler warning when comparing struct to null

喜你入骨 提交于 2019-12-18 19:27:29
问题 Consider the following code: DateTime t = DateTime.Today; bool isGreater = t > null; With Visual Studio 2010 (C# 4, .NET 4.0), I get the following warning: warning CS0458: The result of the expression is always 'null' of type 'bool?' This is incorrect; the result is always false (of type bool ): Now, the struct DateTime overloads the > (greater than) operator. Any non-nullable struct (like DateTime) is implicitly convertible to the corresponding Nullable<> type. The above expression is

How to use .NET reflection to check for nullable reference type

百般思念 提交于 2019-12-18 19:20:17
问题 C# 8.0 introduces nullable reference types. Here's a simple class with a nullable property: public class Foo { public String? Bar { get; set; } } Is there a way to check a class property uses a nullable reference type via reflection? 回答1: This appears to work, at least on the types I've tested it with. You need to pass the PropertyInfo for the property you're interested in, and also the Type which that property is defined on ( not a derived or parent type - it has to be the exact type):

if condition with nullable

♀尐吖头ヾ 提交于 2019-12-18 16:51:46
问题 There is a lot of syntax sugar with Nullable<T> like those: int? parsed to Nullable<int> int? x = null if (x != null) // Parsed to if (x.HasValue) x = 56; // Parsed to x.Value = 56; And more. Why if condition with Nullable doesn't work? if (x) {} It gets Complier error saying can't convert Nullable<bool> to bool . Why it's not being parsed to if (x.HasValue && x.Value == true) or something similar? It's the most obvious usage for Nullable<bool> 回答1: It's the most obvious usage for Nullable

How to check multiple objects for nullity?

别等时光非礼了梦想. 提交于 2019-12-18 13:52:58
问题 Often, I can see a code constructs like following: if(a == null || b == null || c == null){ //... } I wonder if there is any widely used library (Google, Apache, etc.) to check against nullity for multiple objects at once, e.g.: if(anyIsNull(a, b, c)){ //... } or if(allAreNulls(a, b, c)){ //... } UPDATE: I perfectly know how to write it by myself I know it can be the result of the poor program structure but it's not a case here Let's make it more challenging and replace original example with

Checking if Type instance is a nullable enum in C#

末鹿安然 提交于 2019-12-18 10:44:30
问题 How do i check if a Type is a nullable enum in C# something like Type t = GetMyType(); bool isEnum = t.IsEnum; //Type member bool isNullableEnum = t.IsNullableEnum(); How to implement this extension method? 回答1: public static bool IsNullableEnum(this Type t) { Type u = Nullable.GetUnderlyingType(t); return (u != null) && u.IsEnum; } 回答2: EDIT: I'm going to leave this answer up as it will work, and it demonstrates a few calls that readers may not otherwise know about. However, Luke's answer is