我有一個可為空的 c# 10 .net 6 專案,其擴展方法為 ThrowIfNull
using System;
using System.Runtime.CompilerServices;
#nullable enable
public static class NullExtensions
{
public static T ThrowIfNull<T>(
this T? argument,
string? message = default,
[CallerArgumentExpression("argument")] string? paramName = default
)
{
if (argument is null)
{
throw new ArgumentNullException(paramName, message);
}
else
{
return argument;
}
}
}
擴展方法隱式轉換string?為,string但它不適用于其他基本型別,如int?或bool?
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
string? foo = "foo";
string nonNullableFoo = foo.ThrowIfNull(); // success from "string?" to "string"
Console.WriteLine(nonNullableFoo);
bool? baz = true;
bool nonNullableBaz = baz.ThrowIfNull(); // success from "string?" to "string"
Console.WriteLine(nonNullableFoo);
int? bar = 2;
int nonNullableBar = bar.ThrowIfNull(); // error: Cannot implicitly convert type 'int?' to 'int'
Console.WriteLine(nonNullableBar);
}
}
如何使擴展隱式轉換int?和bool??
這是完整的 dotnet fiddle https://dotnetfiddle.net/LiQ8NL
uj5u.com熱心網友回復:
您可以通過為不可為空的參考型別提供一種擴展方法而為非托管(例如 int、bool、...)型別提供另一種擴展方法來實作您的目標。請注意,非托管型別需要強制轉換。
public static class NullExtensions
{
public static T ThrowIfNull<T>(
this T? argument,
string? message = default,
[CallerArgumentExpression("argument")] string? paramName = default
) where T : notnull
{
if (argument is null)
{
throw new ArgumentNullException(paramName, message);
}
else
{
return argument;
}
}
public static T ThrowIfNull<T>(
this T? argument,
string? message = default,
[CallerArgumentExpression("argument")] string? paramName = default
) where T : unmanaged
{
if (argument is null)
{
throw new ArgumentNullException(paramName, message);
}
else
{
return (T)argument;
}
}
}
像這樣使用:
int? foo = 42;
int bar = foo.ThrowIfNull();
Console.WriteLine(bar);
string? baz = "Hello";
string quus = baz.ThrowIfNull();
Console.WriteLine(quus);
// Comment out either this
baz = null;
quus = baz.ThrowIfNull();
// Or this
foo = null;
bar = foo.ThrowIfNull();
uj5u.com熱心網友回復:
在 MS dotnet 檔案中找到了一個可能的選項https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters
約束“where T : struct”型別引數必須是不可為空的值型別
同樣根據這個答案https://stackoverflow.com/a/8745492/2877168不可能使它適用于string?所有其他值型別。所以唯一的辦法就是定義兩個擴展方法。下面是對作品的更新版本string?和int?,bool?等
public static class NullExtensions
{
public static T ThrowIfNull<T>(this T? argument, string? message = default, [CallerArgumentExpression("argument")] string? paramName = default)
where T : struct
{
if (argument is null)
throw new ArgumentNullException(paramName, message);
return (T)argument;
}
public static string ThrowIfNull(this string? argument, string? message = default, [CallerArgumentExpression("argument")] string? paramName = default)
{
if (argument is null)
throw new ArgumentNullException(paramName, message);
return argument;
}
}
其作業版本位于https://dotnetfiddle.net/uBX1w6
uj5u.com熱心網友回復:
1. 使用空合并運算子 ??
要將可空值分配給非空變數,請考慮以下代碼:
int? value = 28;
int result = value ?? -1;
Console.WriteLine($"The result is {result}");
- 輸出:
The result is 28
int? value = null;
int result = value ?? -1;
Console.WriteLine($"The result is {result}");
- 輸出:
The result is -1
2. 編輯代碼
重新排列代碼如下。因此bool?您可以隱式使用該型別:
int? bar = 2;
int nonNullableBar = bar ?? -1;
Console.WriteLine(nonNullableBar);
bool? baz = true;
bool nonNullableBaz = false;
if (baz == true){
nonNullableBaz = true;
}
else if(baz == false){
nonNullableBaz = false;
}
else {
/* Something */
}
3. 資源
- 可空值型別
- 布爾邏輯運算子
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/388588.html
