Code | Title |
---|---|
CS0458 | 運算式的結果一律是 'null' |
CS0464 | 與 struct 類型的 null 進行比較,一律會產生 'false' |
CS0472 | 運算式的結果一律會相同,因為此類型的值絕對不會等於 'null' |
CS1720 | 運算式一律會造成 System.NullReferenceException,因為類型的預設值為 null |
CS8073 | 運算式的結果一律會相同,因為此類型的值絕對不會等於 'null' |
CS8597 | 擲回值可能為 null。 |
CS8600 | 正在將 Null 常值或可能的 Null 值轉換為不可為 Null 的型別。 |
CS8601 | 可能有 Null 參考指派。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
<LangVersion>7.3</LangVersion> | |
</PropertyGroup> | |
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | |
<WarningLevel>3</WarningLevel> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
Task taskA = Task.Run(DoSomething); | |
var cancelManager = new CancellationTokenSource(); | |
Task taskB = taskA.ContinueWith( | |
_ => Console.WriteLine("這裡不會執行"), | |
cancelManager.Token); | |
Task taskC = taskB.ContinueWith( | |
_ => Console.WriteLine("TaskC 執行完畢。")); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void btnSolution3_Click(object sender, EventArgs e) | |
{ | |
var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); | |
var task = GetStringAsync(); | |
task.ContinueWith(t => label1.Text = t.Result, uiScheduler); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
Task taskA = Task.Run( | |
() => Div(10, 5) ); // 若把第二個參數改成 0,接續的工作會變成 taskOnFailed。 | |
Task taskOnFailed = taskA.ContinueWith( | |
antecedentTask => | |
{ | |
Console.WriteLine("Task A 已失敗! IsFaulted={0}", antecedentTask.IsFaulted); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
var taskA = Task.Run(() => Console.WriteLine("起始工作....")); | |
Task taskB = taskA.ContinueWith( antecedentTask => | |
{ | |
Console.WriteLine("從 Task A 接續 Task B."); | |
System.Threading.Thread.Sleep(4000); // 等待幾秒 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
webDownloadTask.ContinueWith(task => | |
{ | |
string content = task.Result; | |
Console.WriteLine($"網頁內容長度:{content.Length}"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void button1_Click(object sender, EventArgs e) | |
{ | |
var uiContext = SynchronizationContext.Current; | |
GetStringAsync().ContinueWith(task => | |
{ | |
uiContext.Post(delegate | |
{ | |
label1.Text = task.Result; | |
}, null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Program | |
{ | |
static async Task Main() | |
{ | |
var url = "https://www.huanlintalk.com"; | |
var content = await MyDownloadPageAsync(url); | |
Console.WriteLine("網頁內容總共為 {0} 個字元。", content.Length); | |
} | |
static async Task<string> MyDownloadPageAsync(string url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Program | |
{ | |
static void Log(int num, string msg) | |
{ | |
Console.WriteLine("({0}) T{1}: {2}", | |
num, Thread.CurrentThread.ManagedThreadId, msg); | |
} | |
static async Task Main() | |
{ |