所以我有这门课:
public class Foo<T> where T : ???
{
private T item;
public bool IsNull()
{
return item == null;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我正在寻找一种类型约束,允许我将所有内容都用作类型参数null.这意味着所有引用类型以及所有Nullable(T?)类型:
Foo<String> ... = ...
Foo<int?> ... = ...
Run Code Online (Sandbox Code Playgroud)
应该是可能的.
使用class类型约束只允许我使用引用类型.
附加信息:
我正在编写管道和过滤器应用程序,并希望使用null引用作为传递到管道的最后一项,以便每个过滤器可以很好地关闭,进行清理等...
你能解释一下,为什么Console.WriteLine写空行(Console.WriteLine(null)给我编译错误)以及为什么没有NullReferenceException(甚至a+=1不应该提高它)?
int? a = null;
a++; // Why there is not NullReferenceException?
Console.WriteLine(a); // Empty line
Run Code Online (Sandbox Code Playgroud) 考虑以下方法
- (void)methodWithArg:(NSString *)arg1 andArg:(NSString *)arg2 completionHandler:(void (^)(NSArray *results, NSError *error))completionHandler;
Run Code Online (Sandbox Code Playgroud)
使用new nonnull和nullable annotation关键字,我们可以丰富它如下:
- (void)methodWithArg:(nonnull NSString *)arg1 andArg:(nullable NSString *)arg2 completionHandler:(void (^)(NSArray *results, NSError *error))completionHandler;
Run Code Online (Sandbox Code Playgroud)
但是我们也得到了这个警告:
指针缺少可为空类型说明符(__nonnull或__nullable)
它指的是第三个参数(块1).
本文档未介绍如何指定块参数的可为空性的示例.它逐字陈述
只要类型是简单对象或块指针,就可以在打开括号后立即使用非下划线形式为nullable和nonnull.
我尝试将两个关键字中的一个放在块中(在任何位置),没有任何运气.还尝试了下划线前缀变体(__nonnull和__nullable).
因此我的问题是:如何为块参数指定可空性语义?
我的模型有一个必须可以为空的布尔值
public bool? Foo
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
所以在我的Razor cshtml中
@Html.CheckBoxFor(m => m.Foo)
Run Code Online (Sandbox Code Playgroud)
除了不起作用.也没有(bool)施放它.如果我做
@Html.CheckBoxFor(m => m.Foo.Value)
Run Code Online (Sandbox Code Playgroud)
这不会产生错误,但在发布时它不会绑定到我的模型,并且foo设置为null.什么是在页面上显示Foo并使其在帖子上绑定到我的模型的最佳方式?
在C#中,类型实例变量的默认值是int?什么?
例如,在以下代码中,MyNullableInt如果从未明确分配,将具有什么值?
class MyClass
{
public int? MyNullableInt;
}
Run Code Online (Sandbox Code Playgroud)
(似乎答案几乎肯定是null或者0,但是那些是什么?)
在C#中,假设您希望在此示例中从PropertyC中提取值,而ObjectA,PropertyA和PropertyB都可以为null.
ObjectA.PropertyA.PropertyB.PropertyC
如何使用最少量的代码安全地获取PropertyC?
现在我会检查:
if(ObjectA != null && ObjectA.PropertyA !=null && ObjectA.PropertyA.PropertyB != null)
{
// safely pull off the value
int value = objectA.PropertyA.PropertyB.PropertyC;
}
Run Code Online (Sandbox Code Playgroud)
做更像这样的事情会很好(伪代码).
int value = ObjectA.PropertyA.PropertyB ? ObjectA.PropertyA.PropertyB : defaultVal;
Run Code Online (Sandbox Code Playgroud)
可能甚至进一步崩溃与空合并运算符.
编辑最初我说我的第二个例子就像js,但是我把它改成了psuedo-code,因为它被正确地指出它在js中不起作用.
c# nullable nullreferenceexception null-conditional-operator
我读上的C#语言规范条件逻辑运算符 ||和&&,也被称为短路逻辑运算符.对我来说,似乎不清楚这些是否存在可空的布尔值,即操作数类型Nullable<bool>(也是书面的bool?),所以我尝试使用非动态类型:
bool a = true;
bool? b = null;
bool? xxxx = b || a; // compile-time error, || can't be applied to these types
Run Code Online (Sandbox Code Playgroud)
这似乎解决了这个问题(我无法清楚地理解规范,但假设Visual C#编译器的实现是正确的,现在我知道).
但是,我也想尝试dynamic绑定.所以我尝试了这个:
static class Program
{
static dynamic A
{
get
{
Console.WriteLine("'A' evaluated");
return true;
}
}
static dynamic B
{
get
{
Console.WriteLine("'B' evaluated");
return null;
}
}
static void Main()
{
dynamic x = A | B;
Console.WriteLine((object)x);
dynamic y …Run Code Online (Sandbox Code Playgroud) 为什么这不起作用?看起来像有效的代码.
string cert = ddCovCert.SelectedValue;
int? x = (string.IsNullOrEmpty(cert)) ? null: int.Parse(cert);
Display(x);
Run Code Online (Sandbox Code Playgroud)
我该怎么编码呢?该方法采用Nullable.如果下拉列表中选择了一个字符串,我需要将其解析为一个int,否则我想将null传递给该方法.
我如何检查Type是否是C#中可以为空的枚举
Type t = GetMyType();
bool isEnum = t.IsEnum; //Type member
bool isNullableEnum = t.IsNullableEnum(); How to implement this extension method?
Run Code Online (Sandbox Code Playgroud) 我有两个int类型的变量?(或如果你愿意,可以为Nullable <int>).我想对两个变量进行大于或等于(> =)的比较,但事实证明,如果两个变量都为null,则返回false,而显然==运算符返回true.
有人可以向我解释为什么这是合乎逻辑的,因为> =运算符的语义定义包含单词"or"?
nullable ×10
c# ×8
.net ×1
block ×1
comparison ×1
dynamictype ×1
enums ×1
generics ×1
integer ×1
null ×1
objective-c ×1
operators ×1
razor ×1
string ×1
xcode ×1