![](/img/trans.png)
[英]Access abstract static interface member
如何從接口IFoo或實例 object 從FooImpl獲取已實現的Name ?public interface IFoo { static abstract string Name { get; } } public class FooImpl : IFoo { public s ...
[英]Access abstract static interface member
如何從接口IFoo或實例 object 從FooImpl獲取已實現的Name ?public interface IFoo { static abstract string Name { get; } } public class FooImpl : IFoo { public s ...
[英]ModelState.IsValid is false when I have a nullable parameter and send it as null
這里是可空參數 你看到的是CreditCard的code 用戶注冊時可以跳過輸入信用卡信息所以會是null credit card 正如你在這里看到的那么如何跳過 null 信用卡的驗證 ...
[英]Creating a "ref tuple"
我正在嘗試使用 C# 11 ref 結構/字段來創建一個“ref 元組”,這是一種包含多個ref的類型。 我的想法是,然后我可以使用ref初始化RefTuple<T1, T2>類型的 object 並將其傳遞給 function,然后 function 可以更改Item1等,這將修改初 ...
[英]Passing a reference to a value in a struct in C# 11
我無法傳遞對 C# 結構中值的引用 11. 我該怎么做? 未處理的異常。 System.NullReferenceException:Object 引用未設置為 object 的實例。在 path\Program.cs 中的 CustomRef..ctor(Double& number): ...
[英]C# 11 escape rules for ref parameters: ref int vs Span<int>
為什么下面的代碼在C# 11編譯不通過? 它產生兩個編譯錯誤: 錯誤 CS9077:無法通過 ref 參數按引用“arg”返回參數; 它只能在返回語句中返回。 錯誤 CS8347:無法在此上下文中使用“Span.Span(ref int)”的結果,因為它可能會在其聲明 scope 之外公開參數“r ...
[英]Is there a way to use different accessibility modifier for a setter in C#11 when using the "required" keyword?
使用“required”關鍵字時,有沒有辦法在 C#11 中為 setter 使用不同的可訪問性修飾符? public class Person { public required string FirstName { get; protected ...
[英]Unexpected behavior of references in a node graph
我正在嘗試創建一個算法,其中包括創建一個節點圖(而且我沒有解決該算法尚未完成和運行的事實)。 我從文件中獲取輸入,然后在圖中創建新節點或通過在節點之間創建新連接來調整現有節點。 我的計划是無論距離多遠,我都可以從任何節點到達任何其他節點。 當我編輯“燈”字段時,出現錯誤,一個鄰居節點看到仍然有一些燈 ...
[英]Alternative to the new C# 11 `required` modifier in C# 10 and earlier
.NET 7 和 C# 11 引入了新的修飾符required 。 文檔說: required 修飾符表示它所應用的字段或屬性必須由所有構造函數或使用 object 初始化程序初始化。 任何初始化該類型的新實例的表達式都必須初始化所有必需的成員。 但是,如果我使用的是 C#10 或更早版本,那么 ...
[英].NET 7 Error CS0118 'UserService' is a namespace but is used like a type
我的服務位於項目中名為Services的文件夾下,並且我已將與用戶相關的部分分組在名為UserService的文件夾中。 在該文件夾下,我有一個名為IUserService.cs的接口和一個名為UserService.cs的 class,其中包含基本的用戶身份驗證方法。 這是文件夾結構的圖片: 為 ...
[英]How can I use a common initializer for init properties in a base class?
考慮 C# 11 中的以下類: 初始化派生 class 的標准方法是: 這很好用,但如果基類 class 有很多屬性或有很多派生類,就會變得乏味。 有沒有什么方法可以為基類 class 使用一個通用的集中式初始化程序,以便只需要為派生類初始化額外的屬性? 類似的東西(發明的語法如下): 如果沒有這 ...
[英]Generic interfaces which type implements another interface
有人可以解釋為什么它不起作用。 我有兩個數據庫上下文。 以及一種具有不同返回類型和不同查詢的通用方法。 執行: 在 output 中出現編譯錯誤: 錯誤(活動)CS0738“FetchService”未實現接口成員“IDataFetcher.GetData()”。 “FetchService.Ge ...
[英]C# 11, Raw String Literals - Line does not start with the same whitespace as the closing line
嘗試使用新引入的 C# 11 功能Raw string literals ,如您在以下示例中所見:_logger.LogCritical($""" new value: {value} computed new value : {value / 100} old value: {Uw ...
[英]What is default duration of CacheOutput() middleware in ASP.NET Core 7.0, .NET 7.0
今天學習了ASP.NET Core 7.0 (.NET 7.0)中的CacheOutput中間件。 我研究了很多次,但找不到默認的緩存持續時間。 CacheOutput()中間件的默認緩存持續時間是多少? 謝謝。 ...
[英]linq expression for ExecuteUpdateAsync
我以極大的熱情在 EF Core 7 中找到了 ExecuteDeleteAsync 和 ExecuteUpdateAsync。 它們有助於使我的代碼更加簡單和快速。 批量刪除或更新1-2個字段無需使用自制程序。 無論如何,在運行時應該選擇要更新的確切表和數據庫字段的情況。 我可以使用數據庫表: 我 ...
[英]The new feature `required' of c# 11 have a compilation error in multi target project including net7,net472
環境:vs 2022 v 17.4.0,安裝.NET7 sdk 我有一個多目標項目net7.0.net472 : 我嘗試使用 c# 11 中所需的新功能,如下所示: 我在 .net472 中遇到編譯錯誤: 錯誤 CS0656 缺少編譯器所需的成員“System.Runtime.CompilerSe ...
[英]C# 11 - What have we gained by adding IAdditionOperators to this method?
我剛剛將 Visual Studio 2022 升級到 .NET7,其中包括 C# 11。我有興趣嘗試新的static 抽象接口方法,因此按照那里的教程進行操作。 本文介紹了如何定義使用新的IAdditionOperators<>接口的Point和Translation記錄...pub ...
[英]How to implement partial class in different file for file scoped classes in C#?
對於 C# 11 中的文件范圍類型,將無法在不同的文件中為范圍為 class 的文件實現部分 class。 這將是文件范圍類的例外嗎? 因為這樣會失去partial class的目的。 參考: https://learn.microsoft.com/en-us/do.net/csharp/lang ...
[英]Access Abstract Interface Member C#11
我正在嘗試從所有內置的INumber<T>類中獲取新的AdditiveIdentity屬性。 所以我從int開始(應該返回 0)。 但是,我發現實際上很難達到這個值......(我已經安裝了 .NET 7 Preview 7)。 我本以為我能夠做到:int additiveIdenti ...
[英]why is IParseable<T> need to be recursive?
為什么IParseable<T>將T: IParseable<T>約束放在 T 上? 這個遞歸約束需要什么? ...
[英]What is the purpose of the 'scoped' keyword
在查看新DefaultInterpolatedStringHandler的源代碼時,我注意到ReadOnlySpan使用scoped關鍵字進行了注釋。 我能找到的唯一文檔是here 。 但是,我無法弄清楚以下代碼片段之間的實際區別是什么。 我假設使用scoped關鍵字,不允許將參數傳遞給被調用的方法 ...