我有一个关于如何确定对象的Nullable属性类型的问题.
具有DateTime属性的ObjectA?CREATEDATE;
当我迭代它的属性,如下面的代码,我如何检查属性是否是Nullable DateTime类型?
谢谢
foreach (PropertyInfo pi in ObjectA.GetType().GetProperties())
{
//do the compare here
}
Run Code Online (Sandbox Code Playgroud) 我想知道?C#中的含义是什么?
我看到的东西是:DateTime?或者int?.我想这是特定于C#4.0的?
我不能在谷歌找它,因为我不知道这个东西的名字.
问题是我使用的日期时间和我有很多投的错误(从DateTime到DateTime?).
谢谢
我在JPA 2.0 FR规范11.1.37中遇到了这个例子.OneToOne注释,第403页:
@OneToOne(optional=false)
@JoinColumn(name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
public CustomerRecord getCustomerRecord() { return customerRecord; }
Run Code Online (Sandbox Code Playgroud)
有什么理由我应该放在@OneToOne(optional=false)同一时间@JoinColumn(... nullable=false)吗?
这两个声明不一样吗?他们中的一个不是多余的吗?
它们都用于DDL模式生成吗?
我只是想知道下两个语句之间有什么区别:
导致NullReferenceException - 没关系.
object test1 = default(int?);
object result = test1.ToString();
Run Code Online (Sandbox Code Playgroud)返回一个空字符串"",为什么?
object test2 = default(int?).ToString();
Run Code Online (Sandbox Code Playgroud)这与2相同.
int? test3 = null;
if (test3.ToString() == string.Empty) //returns true
{
Console.WriteLine("int? = String.Empty, isn't it strange?").
}
Run Code Online (Sandbox Code Playgroud)而且只是为了好玩 - 我可以证明bool可以等于int值(嗯,怎么样?bool只能是false,或者是true,而int永远不会那样).
if (default(int?).ToString() == default(bool?).ToString()) //returns true because both are empty strings
{
Console.WriteLine("int = bool");
}
Run Code Online (Sandbox Code Playgroud)注意: default(int?)返回null.
我昨天升级到了Xcode 6.3.从那以后,我一直无法构建任何具有Parse.framework的东西.对于PFConstants.h,我得到错误
nullability specifier '_nullable' cannot be applied to non-pointer
我之前从未见过这个错误,除了更新到6.3之外什么都没有改变.有任何想法吗?
现在在objective-c中有两个新的注释:nonnull和nullable.
我应该将哪一个用于init方法的返回类型规范?
- (instancetype)init {
if (self = [super init]) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
可空的声音:
有一个"if"来检查[super init]返回什么,并且不能保证它永远不会返回nil.
nonnull的声音:
当init返回nil并且我从不检查它时,我不知道真实的情况.
所以我有这样的事情
public string? SessionValue(string key)
{
if (HttpContext.Current.Session[key].ToString() == null || HttpContext.Current.Session[key].ToString() == "")
return null;
return HttpContext.Current.Session[key].ToString();
}
Run Code Online (Sandbox Code Playgroud)
哪个不编译.
如何返回可以为空的字符串类型?
可能重复:
Nullable类型不是可空类型?
在以下代码中:
DateTime? dt = DateTime.Now;
MessageBox.Show(dt.GetType().ToString());
Run Code Online (Sandbox Code Playgroud)
消息框显示"System.DateTime",而不是Nullable<DateTime>.以下内容也返回false(因为GetType错误):
if (dt.GetType().IsAssignableFrom(typeof(DateTime?)))
...
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,使用DateTime?或Nullable<DateTime>没有区别)在监视窗口中,您有"类型"列显示正确的类型(System.DateTime?).
在我的代码中,我引用了dt作为一个object,所以我需要正确地获取底层类型.我怎样才能做到这一点?
我有一个数据库查询,它将返回NULL或布尔(位)值.
我希望将此值存储Nullable<bool>在C#中的类型变量中.
我似乎无法找到一个可接受的explict强制转换和转换组合,以简单的方式执行此操作,而不会抛出异常.
它可以用一条可读行完成吗?
编辑:代码请求
private Nullable<bool> IsRestricted;
...//data access
IsRestricted = (bool?)DataBinder.GetPropertyValue(dataObj, "IsRestricted");
Run Code Online (Sandbox Code Playgroud)
也许
IsRestricted = (bool?)(bool)DataBinder.GetPropertyValue(dataObj, "IsRestricted");
Run Code Online (Sandbox Code Playgroud) 我喜欢pattern-matching在nullable intie 上使用int?:
int t = 42;
object tobj = t;
if (tobj is int? i)
{
System.Console.WriteLine($"It is a nullable int of value {i}");
}
Run Code Online (Sandbox Code Playgroud)
但是,这会导致以下语法错误:
'i)'标有红色波浪线.
使用旧运算符时表达式编译is:
int t = 42;
object tobj = t;
if (tobj is int?)
{
System.Console.WriteLine($"It is a nullable int");
}
string t = "fourty two";
object tobj = t;
if (tobj is string s)
{
System.Console.WriteLine($@"It is a …Run Code Online (Sandbox Code Playgroud)