小编Wou*_*ter的帖子

如何将 INumber<T> 与 int 相乘?

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)

c# math numbers .net-7.0 .net-generic-math

20
推荐指数
3
解决办法
2006
查看次数

将字符串转换为序数大写或小写

是否可以将字符串转换为序数大写或小写.类似于不变量.

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)

c# string lowercase ordinal uppercase

14
推荐指数
1
解决办法
1515
查看次数

在 if 语句中使用 var

鉴于:

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#

5
推荐指数
1
解决办法
217
查看次数

在宽度或高度为零的情况下,Rect IsEmpty不返回true

在C#IsEmpty(on System.Windows.Rect)中不返回truewhere WidthHeight为零.在MSDN中,它被注意到但没有(明确地)解释:

不要使用此属性来测试零区域; 面积为零的矩形不一定是空矩形.有关更多信息,请参阅Empty属性.

为什么不将IsEmpty实现为:

public bool IsEmpty
{
    return Width == 0 || Height == 0;
}
Run Code Online (Sandbox Code Playgroud)

当前实现的用例是什么?

c# rect

2
推荐指数
2
解决办法
724
查看次数

无证行为!和?? 操作者

应该怎么读

!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也没有编译.

有人知道一些解释或文件吗?

.net c#

0
推荐指数
2
解决办法
268
查看次数

标签 统计

c# ×5

.net ×1

.net-7.0 ×1

.net-generic-math ×1

lowercase ×1

math ×1

numbers ×1

ordinal ×1

rect ×1

string ×1

uppercase ×1