想要一个简单但有效的方法,在编译时不知道T时从Nullable获取值.
到目前为止有这样的事情:
public static object UnwrapNullable(object o)
{
if (o == null)
return null;
if (o.GetType().IsGenericType && o.GetType().GetGenericTypeDefinition() == typeof(Nullable<>))
return ???
return o;
}
Run Code Online (Sandbox Code Playgroud)
什么都可以在这里完成而不需要动态代码生成?
用于.NET 2.0
以下是一些入门示例代码:
class Foo
{
public bool? IsValid { get; set; }
}
// later in some other function...
void DoStuff( Foo myFoo )
{
myControlState.Visible = myFoo.IsValid.HasValue ? myFoo.IsValid.Value : false;
}
Run Code Online (Sandbox Code Playgroud)
我遇到很多情况,我必须使用像上面这样的三元运算符来正确使用可空的bool.如果有一种稍微简单的方法来获取bool的值而不抛出异常,那将是很好的.上面的代码似乎是直截了当的,但是有更复杂的情况,这最终会成为很多代码.我希望有一些简单的东西:
myControlState.Visible = GetNullableValue<bool>( myFoo );
Run Code Online (Sandbox Code Playgroud)
有没有人有三元运算符的更清洁的替代品?
我有一份清单Foo's.
Foo有一种CreateDate类型DateTime?
我想得到最老的Foo.我尝试了直观的方式,但我得到了一个可能System.InvalidOperationException.
DateTime oldestFoo =
(from f in allFoos where f.CreateDate.HasValue select f.CreateDate.Value).Min();
Run Code Online (Sandbox Code Playgroud)
有没有我错过的东西或者这种方式不可能?
我可以在没有LinQ的情况下这样做,但我也想学习另一种方法.
DateTime oldestFoo = DateTime.MaxValue;
foreach (var foo in allFoos)
{
if (foo.CreateDate.HasValue && oldestFoo > foo.CreateDate)
{
oldestFoo = (DateTime)foo.CreateDate;
}
}
Run Code Online (Sandbox Code Playgroud) 谁能帮助我了解才知道之间的区别decimal和decimal?或int和int??
我何时应该使用它们以及使用它们中的任何一个是积极的.
class Program {
static bool? a = null;
static bool b = false;
static void Main( string[] args ) {
//1
if( a!=null ) {
b = (bool) a;
}
//2
if( a!=null && (b=(bool) a) ) { }
}
}
Run Code Online (Sandbox Code Playgroud)
案例#1和案例#2之间有什么区别吗?
我是c#的新手,请在我错误的地方纠正我
我正在尝试创建一个具有Nullable属性的类但是它在构建类时出错我的类看起来像
public class vwAcdAdmissionWithvwAcdAdmissionSessionDetailWithAllMaster1
{
public Nullable<string> Name { get; set; }
public Nullable<string> StudentNameWithLedgerName { get; set; }
public Nullable<decimal> AASDid { get; set; }
public Nullable<decimal> StudentLedgerId { get; set; }
public Nullable<string> LedgerName { get; set; }
public Nullable<decimal> SessionId { get; set; }
public Nullable<string> ScholarNo { get; set; }
public Nullable<decimal> ClassId { get; set; }
public Nullable<decimal> SectionId { get; set; }
public Nullable<decimal> MediumId { get; set; }
public Nullable<decimal> StreamId { …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的Android SQLite表定义:
create table MyTable(..., myDate integer, ...);
Run Code Online (Sandbox Code Playgroud)
稍后在我的代码中,我查询此表以通过游标检索myDate的值:
long dateInstant = cursor.getLong(cursor.getColumnIndexOrThrow("myDate"));
Run Code Online (Sandbox Code Playgroud)
但是,我想知道这是否是最佳方法.如果未设置该值(即数据库为null),则上述方法返回0,这恰好也是有效值.我需要区分有效值和空值.另外,从javadoc getLong:
结果以及当列值为null时,此方法是否抛出异常,列类型不是整数类型,或者整数值超出范围[Long.MIN_VALUE,Long.MAX_VALUE]是实现定义的.
这似乎表明,如果我正确地读到这一点,那么在未来的某个时刻,Android中提供的SQLite实现可能会选择抛出异常而不是返回0.我正在寻找跨Android版本的一致解决方案.
简而言之,我如何能够以一种允许我区分有效值和null的方式稳健地处理从列中检索整数数据?
我相信一个对象(引用类型)默认是可以为空的,即它可以有一个值或为null.为什么我要将它明确地声明为可以为空的对象?
假设我有一个包含x成员的数组,我想创建另一个具有相同Length(x)和相同整数值的数组:
int[] arr = new int[x];
int?[] nullable_arr = arr;
Run Code Online (Sandbox Code Playgroud)
我不能这样做:
int?[] arr = new int[x];
Run Code Online (Sandbox Code Playgroud)
是否存在显式转换为可空类型数组或我不知道的内容?
是的,我可以简单地在循环中转换每个值,但我问是否已经有一个简单的方法.
试图更好地理解为什么这是一种语言功能:
我们有:
public static DateTime? Date { get; set; }
static void Main(string[] args)
{
Date = new DateTime(2017, 5, 5);
Console.WriteLine(Date.Value.Date);
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
为什么我需要使用Value从可空类型中获取值?它不像在调用Date之前检查null,如果值为null,它将抛出NullReference异常.我明白了.HasValue可以工作,
但我不确定为什么我们需要.每个nulllable类型的值?