Tag:

转换小数? 加倍?

我想知道从一个可空类型转换为另一个“兼容”可空类型的最佳方式(在更安全和简洁的意义上)是什么。 具体来说,从十进制转换? 加倍? 可以使用: public double? ConvertToNullableDouble(decimal? source) { return source.HasValue ? Convert.ToDouble(source) : (double?) null; } 有没有更好的方法来做到这一点? 也许利用标准转换?

我可以在一行中从DBNull转换为Nullable Bool吗?

我有一个数据库查询,它将返回NULL或布尔(位)值。 我希望将此值存储在C#中Nullable类型的变量中。 我似乎无法找到一个可接受的explict强制转换和转换组合,以简单的方式执行此操作,而不会抛出exception。 可以用一条可读行完成吗? 编辑:代码请求 private Nullable IsRestricted; …//data access IsRestricted = (bool?)DataBinder.GetPropertyValue(dataObj, “IsRestricted”); 也许 IsRestricted = (bool?)(bool)DataBinder.GetPropertyValue(dataObj, “IsRestricted”);

什么是使用Nullable.GetUnderlyingType,如果typeof(int?)是Int32?

为什么是typeof int? 一个Int32 int? x = 1; Console.WriteLine(x.GetType().Name); 如果可以,那么Nullable.GetUnderlyingType的用途是什么?

C#Nullable类型的编码实践

我从未在C#代码中使用过可空类型。 现在我决定通过在我的代码中引入可空类型来改变我的编码实践。 在应用程序编程的情况下,在从普通数据类型转换为可空数据类型的同时,应该对编码实践进行哪些主要更改? 应该注意哪些方面? 我应该经常注意哪些要点?

Nullable通过HasValue = false的reflection创建,仅给出类型为“Type”的参数

给定一个Nullable的类型参数,如何创建具有HasValue = false的该类型的实例? 换句话说,填写此代码: //type is guaranteed to implement Nullable public static object Create(Type type) { //Instantiate a Nullable with reflection whose HasValue = false, and return it } 我的尝试,它不起作用(它抛出NullReferenceException ),因为没有默认的构造函数: static void Main(string[] args) { Console.WriteLine(Create(typeof(Nullable))); Console.ReadLine(); } //type is guaranteed to be implement from Nullable public static object Create(Type type) { //Instantatie a […]

Nullable 实现

我正在尝试实现Nullable类型。 但是下面提到的代码不支持valuetype数据类型的空值。 using System; using System.Runtime; using System.Runtime.InteropServices; namespace Nullable { [Serializable, StructLayout(LayoutKind.Sequential)] public struct Nullable where T : struct { private bool hasValue; public bool HasValue { get { return hasValue; } } internal T value; public Nullable(T value) { this.value = value; this.hasValue = true; } public T Value { get { if […]

LINQ 2实体,如何在linq查询中检查DateTime.HasValue

我有这个方法,它应该从名为ENTRY的表(&EntitySet)中获取最新的消息 ///方法获取“days”作为参数,用于新的TimeSpan(天,0,0,0); !! using (Entities db = new Entities()) { var entries = from ent in db.ENTRY where ent.DATECREATE.Value > DateTime.Today.Subtract(new TimeSpan(days, 0, 0, 0)) select new ForumEntryGridView() { id = ent.id, baslik = ent.header, tarih = ent.entrydate.Value, membername = ent.Member.ToString() }; return entries.ToList(); } 这里DATECREATED在数据库中是Nullable。 我不能在这个查询中放置“if”…任何方法来检查它? Thx提前

Nullable类型之间的转换

.NET 4.0中是否有转换器支持可空类型之间的转换,以缩短如下指令: bool? nullableBool = GetSomething(); byte? nbyte = nullableBool.HasValue ? (byte?)Convert.ToByte(nullableBool.Value) : null;

Nullable DateTime?

如何为可空日期时间创建setter和getter属性。 例如: private DateTime mTimeStamp; public DateTime TimeStamp { get { return mTimeStamp; } set { mTimeStamp = value; } } 可空属性是否支持setter和getter或者我是否将其声明为public? private DateTime? mTimeStamp; public DateTime TimeStamp { }

当T为什么时,为什么Nullable 不是有效的自定义属性参数?

如果我有这样的枚举 public enum Hungry { Somewhat, Very, CouldEatMySocks } 和这样的自定义属性 public class HungerAttribute : Attribute { public Hungry HungerLevel { get; set; } public Hungry? NullableHungerLevel { get; set; } } 我可以做这个 [Hunger(HungerLevel = Hungry.CouldEatMySocks)] public class Thing1 但我不能这样做 [Hunger(NullableHungerLevel = Hungry.CouldEatMySocks)] public class Thing2 它会生成一个错误,指出“’NullableHungerLevel’不是有效的命名属性参数,因为它不是有效的属性参数类型”。 为什么不允许这样做? 据我所知,从根本上说它不在被接受的类型列表中。 有效类型似乎是基元,枚举,字符串,类型和前面类型的一维数组。 当Nullable出现时,这只是一个没有更新的旧规则吗?