C# Language
C # 5.0 Kenmerken
Zoeken…
Syntaxis
Async & wachten
openbare taak MyTask Async () {doSomething (); }
wacht op MyTaskAsync ();
publieke taak
<string>
MyStringTask Async () {return getSomeString (); }string MyString = wachten op MyStringTaskAsync ();
Beller Informatie Attributen
public void MyCallerAttributes (string MyMessage,
[CallerMemberName] string MemberName = "",
[CallerFilePath] string SourceFilePath = "",
[CallerLineNumber] int LineNumber = 0)
Trace.WriteLine ("My Message:" + MyMessage);
Trace.WriteLine ("Member:" + MemberName);
Trace.WriteLine ("Bronbestandspad:" + SourceFilePath);
Trace.WriteLine ("Line Number:" + LineNumber);
parameters
Methode / Modifier met parameter | Details |
---|---|
Type<T> | T is het retourtype |
Opmerkingen
C # 5.0 is gekoppeld aan Visual Studio .NET 2012
Async & wachten
async
en await
zijn twee operators die zijn bedoeld om de prestaties te verbeteren door threads vrij te maken en te wachten tot de bewerkingen zijn voltooid voordat ze verder gaan.
Hier is een voorbeeld van het ophalen van een string voordat deze de lengte retourneert:
//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);
}
Hier is nog een voorbeeld van het downloaden van een bestand en het afhandelen van wat er gebeurt als de voortgang is veranderd en wanneer de download is voltooid (er zijn twee manieren om dit te doen):
Methode 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!")
}
Methode 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();
}
Beller Informatie Attributen
CIA's zijn bedoeld als een eenvoudige manier om attributen te verkrijgen van wat de gerichte methode ook wordt genoemd. Er is echt maar 1 manier om ze te gebruiken en er zijn maar 3 attributen.
Voorbeeld:
//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);
}
Voorbeelduitvoer:
//Message: Show my attributes.
//Member: doProcess
//Source File Path: c:\Path\To\The\File
//Line Number: 13