因为C# 8.0可空引用类型是可能的。如果启用了它们,则必须追加?到引用类型,使其为空。
我现在的问题是,如何强制使用params关键字的方法参数不可为空。
示例:
#nullable enable
public void MyMethod(params int[] foo)
{
// foo can still be null
// How can I ensure that foo is not null at compile-time
}
MyMethod(null); // valid, but why?
另一个类似的问题是:为什么params int[] foo一开
class Another {
public void method(Object o) {
System.out.println("This is in method which takes object");
}
public void method(String s) {
System.out.println("This is method which takes string");
}
}
public class NewClass {
public static void mai
我最近开始学习oracle和sql。在学习过程中,我遇到了几个问题,我的朋友在一次面试中被问到。
SELECT *
FROM Employees
WHERE NULL IS NULL;
此查询将生成Employees表中的所有行。正如我所理解的,Oracle在列中搜索数据,因此,NULL,这里是否将其视为列名?当我说Oracle在列中搜索数据时,是否正确?为什么Oracle会给出这个查询中的所有行?在WHERE子句中,条件的左侧不是必须是列名吗?它不应该抛出一个错误吗?
SELECT *
FROM Employees
WHERE NULL = NULL;
不提供选定的行。
嗯,我知
选择sum(a)从求和开始,我们不需要检查空值,但是在加法中我们需要检查,谁能告诉我为什么?示例
我们不需要检查空值。
select sum(a) from
(
select 1 a, null b from dual union
select null, 1 b from dual
)
但是,我们需要检查,如果我们不检查它将是空的
select sum(nvl(a,0) + nvl(b,0)) from
(
select 1 a, null b from dual union
select null, 1 b from dual
)
我创建了一个通过ref接受对象作为参数的类。当它在它的构造中接受这个对象时,它是空的。为什么它保持为空(未初始化)?
// example code, not real
class Test{
object parent;
public Test (ref object _parent)
{
parent = _parent;
}
}
object n = null;
Test t = new Test (ref n);
n = new Something ();
当我用一个不同的类()尝试它时,它仍然是空的
我正在用NET5.0测试c# 9.0,有些东西我不明白。我启用了<nullable>enable</nullable>。
如果我写一堂课
public class Potato {
public string Colour { get; set; }
public string Flavour { get; set; }
}
我收到警告CS8618: Non-nullable propery 'Colour' must contain a non-null value when existing constructor. Consid
我有一个使用params关键字的方法,如下所示:
private void ParamsMethod(params string[] args)
{
// Etc...
}
然后,我使用各种参数组合调用该方法:
// Within the method, args is...
ParamsMethod(); // - a string array with no elements
ParamsMethod(null); // - null (Why is this?)
ParamsMetho
为什么在map函数之前检查过的可空类型在map中不起作用?
function x() {
let y: number | null;
y = null;
if (!y) {
return [];
}
return [1, 2, 3, 4].map((z) => {
// error: y cannot be null
return z + y;
})
}
。
class Program
{
static void Main(string[] args)
{
Console.WriteLine(SomeFunction<int>()==null);
var temp = new SomeClass<int>();
Console.WriteLine(temp.SomeFunction() == null);
Console.ReadKey();
}
static public T? SomeFunction<T>()
fun <T: Any?> function(input: T): T = input
fun function2() {
val n = function(1)!!
}
由于T被声明为可空,所以我希望输出也是可空的,但是Lint会生成Unnecessary non-null assertion (!!) on a non-null receiver of type Int警告。将输出类型签名更改为T?会使警告消失。
为什么输出类型不符合解密的空值?
根据返回类型声明的 (强调“地雷”):
……在默认弱模式下,如果返回的值不是类型,则它们将被强制转换为正确的类型。
这意味着方法returnInt()
class A {
public function returnInt(): int {
return "6a";
}
}
将返回int值6(正如它应该的那样)。
但是,从上面的函数返回null会抛出一个TypeError,尽管可以很容易地将null强制使用(int) null的整数0。
致命错误未登录TypeError:返回A::be ()的返回值必须为整数类型,返回null
为
fun checkLengthA(str : String?): Int = if (str.isNullOrBlank()) 0 else str.length
“只有安全(?)或非空断言(!!)调用才允许在字符串类型的可空接收器上调用?
所有空对象(或空对象)都被isNullOrBlank()捕获,因此str.length中的str对象永远不会为空(或空)。这可以通过使用显式检查替换扩展函数来实现。
fun checkLengthB(str : String?): Int = if (str == null) 0 else str.length
或较少冗长的表达方式:
fun checkL