C# Task<object> return Type

In every signalr or webapi request based application there is a need to indicate the return type of Task<object> along with the async keyword. Let say this sample:

public async Task<MyObject> GetItem(){

MyObject myObject = new MyObject();

myObject = await getSomeWhereThere();

#doSomethingElse

return myObject;

}

In this sample, we all know that this is a asynchronous request from somewhere. Which dictates that myObject will return later after the getSomeWhereThere(); has finished executing. But we need to remind of a very important thing that #doSomethingElse will not be executed in Xamarin if it is not properly used. Let say we did this instead:

public async void GetItem(){

MyObject myObject = new MyObject();

myObject = await getSomeWhereThere();

#doSomethingElse

}

In this code the #doSomethingElse will not be executed because it will only assume that after the getSomeWhereThere(); is executed you don’t mind if #doSomethingElse is still needed. Thus make sure that we use the Task<MyObject> in cases like this so that #doSomethingElse will still be executed.

Author: Thomie Jose San Agustin

He is a Microsoft Certified Professional whom develop different website using ASP .NET MVC and PHP. He can also create systems out of C#. A Magna Cum Laude of Adamson University whom started a Freelance company named i-CT Combinatorics Technology with his friend Calbin Montalban. He accepts both Website and System development and even can help now his colleagues in generating their logic and research.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.