z * 2
我应该如何在最后一行实现 int ( ) 的乘法?
public static TResult Test<TResult>() where TResult : INumber<TResult>
{
TResult x = TResult.AdditiveIdentity;
TResult y = TResult.MultiplicativeIdentity;
TResult z = x * y;
TResult z2 = z * 2; // <--- this gives the CS0019 error "The operator * cannot be applied to operands of type 'TResult' and 'int'
return z2;
}
Run Code Online (Sandbox Code Playgroud)
--- 建议的解决方案是添加一个接口,但它打破了这一点:
IMultiplyOperators<TResult, int, TResult>
public static void Tester()
{
Test<decimal>(); // CS0315 Tye type decimal cannot be used as …
Run Code Online (Sandbox Code Playgroud) 是否可以将字符串转换为序数大写或小写.类似于不变量.
string upperInvariant = "ß".ToUpperInvariant();
string lowerInvariant = "ß".ToLowerInvariant();
bool invariant = upperInvariant == lowerInvariant; // true
string upperOrdinal = "ß".ToUpperOrdinal(); // SS
string lowerOrdinal = "ß".ToLowerOrdinal(); // ss
bool ordinal = upperOrdinal == lowerOrdinal; // false
Run Code Online (Sandbox Code Playgroud)
如何实现ToUpperOrdinal和ToLowerOrdinal?
编辑:如何获取序数字符串表示?同样,如何获得不变的字符串表示?也许这是不可能的,因为在上述情况下它可能是模糊的,至少对于序数表示.
EDIT2:
string.Equals("ß", "ss", StringComparison.InvariantCultureIgnoreCase); // true
Run Code Online (Sandbox Code Playgroud)
但
"ß".ToLowerInvariant() == "ss"; // false
Run Code Online (Sandbox Code Playgroud) 鉴于:
public class Foo
{
public Bar GetBar() => null;
}
public abstract class Bar
{
public abstract void Baz();
}
Run Code Online (Sandbox Code Playgroud)
这有效:
var foo = new Foo();
var bar = foo.GetBar();
if (bar != null)
{
bar.Baz();
}
Run Code Online (Sandbox Code Playgroud)
这也有效:
var foo = new Foo();
if (foo.GetBar() is Bar bar)
{
bar.Baz();
}
Run Code Online (Sandbox Code Playgroud)
但是为什么在 if 语句中使用 var 不起作用呢?
这可以编译但会抛出空引用异常:
if (foo.GetBar() is var bar)
{
bar.Baz(); // <-- bar can still be null?
}
Run Code Online (Sandbox Code Playgroud) 在C#IsEmpty
(on System.Windows.Rect
)中不返回true
where Width
或Height
为零.在MSDN中,它被注意到但没有(明确地)解释:
不要使用此属性来测试零区域; 面积为零的矩形不一定是空矩形.有关更多信息,请参阅Empty属性.
为什么不将IsEmpty实现为:
public bool IsEmpty
{
return Width == 0 || Height == 0;
}
Run Code Online (Sandbox Code Playgroud)
当前实现的用例是什么?
应该怎么读
!nullableboolean ?? false
Run Code Online (Sandbox Code Playgroud)
当nullableboolean == null
它等于false
.
根据:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/
!
结合力强于 ??
它也不同于
!(nullableboolean ?? false)
Run Code Online (Sandbox Code Playgroud)
当nullableboolean == null
它等于true
.
但后来!nullableboolean == null
这似乎没有记载.当然!null
不编译.也许它只能与...一起使用??
.所以也许??
有一个没有文档的对应操作符!
??
,但!null ?? false
也没有编译.
有人知道一些解释或文件吗?