C# Language
C#5.0の機能
サーチ…
構文
非同期&待機中
パブリックタスク MyTask Async (){doSomething(); }
MyTaskAsync();を待ちます。
パブリックタスク
<string>
MyStringTask Async (){return getSomeString(); }string MyString = MyStringTaskAsync()を待機します。
発信者情報の属性
public void MyCallerAttributes(string MyMessage、
[CallerMemberName]文字列MemberName = ""、
[CallerFilePath] string SourceFilePath = ""、
[CallerLineNumber] int LineNumber = 0)
Trace.WriteLine( "私のメッセージ:" + MyMessage);
Trace.WriteLine( "Member:" + MemberName);
Trace.WriteLine( "ソースファイルパス:" + SourceFilePath);
Trace.WriteLine( "行番号:" + LineNumber);
パラメーター
パラメータ付きのメソッド/修飾子 | 詳細 |
---|---|
Type<T> | T は戻り値の型です |
備考
C#5.0はVisual Studio .NET 2012と結合されています
非同期&待機中
async
とawait
は、スレッドを解放し、操作が完了するのを待ってから先に進むことで、パフォーマンスを向上させることを目的とした2つの演算子です。
長さを返す前に文字列を取得する例を次に示します。
//This method is async because:
//1. It has async and Task or Task<T> as modifiers
//2. It ends in "Async"
async Task<int> GetStringLengthAsync(string URL){
HttpClient client = new HttpClient();
//Sends a GET request and returns the response body as a string
Task<string> getString = client.GetStringAsync(URL);
//Waits for getString to complete before returning its length
string contents = await getString;
return contents.Length;
}
private async void doProcess(){
int length = await GetStringLengthAsync("http://example.com/");
//Waits for all the above to finish before printing the number
Console.WriteLine(length);
}
ファイルをダウンロードし、進行状況が変更されたときやダウンロードが完了したときの処理を示す別の例を次に示します(これには2通りの方法があります)。
方法1:
//This one using async event handlers, but not async coupled with await
private void DownloadAndUpdateAsync(string uri, string DownloadLocation){
WebClient web = new WebClient();
//Assign the event handler
web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
web.DownloadFileCompleted += new AsyncCompletedEventHandler(FileCompleted);
//Download the file asynchronously
web.DownloadFileAsync(new Uri(uri), DownloadLocation);
}
//event called for when download progress has changed
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e){
//example code
int i = 0;
i++;
doSomething();
}
//event called for when download has finished
private void FileCompleted(object sender, AsyncCompletedEventArgs e){
Console.WriteLine("Completed!")
}
方法2:
//however, this one does
//Refer to first example on why this method is async
private void DownloadAndUpdateAsync(string uri, string DownloadLocation){
WebClient web = new WebClient();
//Assign the event handler
web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
//Download the file async
web.DownloadFileAsync(new Uri(uri), DownloadLocation);
//Notice how there is no complete event, instead we're using techniques from the first example
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e){
int i = 0;
i++;
doSomething();
}
private void doProcess(){
//Wait for the download to finish
await DownloadAndUpdateAsync(new Uri("http://example.com/file"))
doSomething();
}
発信者情報の属性
CIAは、対象となる方法を呼び出すものから属性を取得する簡単な方法として意図されています。実際には1つの方法しかありませんが、3つの属性しかありません。
例:
//This is the "calling method": the method that is calling the target method
public void doProcess()
{
GetMessageCallerAttributes("Show my attributes.");
}
//This is the target method
//There are only 3 caller attributes
public void GetMessageCallerAttributes(string message,
//gets the name of what is calling this method
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
//gets the path of the file in which the "calling method" is in
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
//gets the line number of the "calling method"
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
//Writes lines of all the attributes
System.Diagnostics.Trace.WriteLine("Message: " + message);
System.Diagnostics.Trace.WriteLine("Member: " + memberName);
System.Diagnostics.Trace.WriteLine("Source File Path: " + sourceFilePath);
System.Diagnostics.Trace.WriteLine("Line Number: " + sourceLineNumber);
}
出力例:
//Message: Show my attributes.
//Member: doProcess
//Source File Path: c:\Path\To\The\File
//Line Number: 13