Pages

C# Features Through the History - Version 5.0


The main features of version 5.0 available for you now!



The version 5.0 of the C# Language was released with Visual Studio 2012, following the version 4.5 of the .NET Framework. They were few, but powerful changes in this version.

How to use the examples 

The examples are available here. You can open the solution and play around. Just uncomment the code and have fun! 


Caller Info Attributes

You can add some attributes for the parameters of your methods, called CallerInfoAttributes. It will help you tracking the information about the methods. There are three types:

  • [CallerMemberName]: used to set the information about caller member name;
  • [CallerFilePath]: used to set the information about the caller's source code file;
  • [CallerLineNumber]: used to set the information about caller's line number.
The next example shows hot it is simple to return information of the caller, using the attributes:

        
		static void SomeExecutionMethod([CallerMemberName] string callerMemberName = "", [CallerFilePath] string callerFilePath = "", 
                                        [CallerLineNumber] int callerLineNumber = 0)
        {
            Log(callerMemberName, callerFilePath, callerLineNumber);
        }

        /**
        Caller Method: ExecuteExample 
        Caller Source Code File: /home/ono/Documents/study/csharpstudy/CSharpStudy/CSharp5.0/1-CallerInfoAttributes.cs 
        Line Number of caller: 10 
        Executed at: 5/24/2020 12:18:25 PM.
        **/
        static void Log(string method, string path, int lineNumber)
        {
            Console.WriteLine($"Some information about the execution\n\n\n  : " +
                $"Caller Method: {method} \n" +
                $"Caller Source Code File: {path} \n" +
                $"Line Number of caller: {lineNumber} \n" +
                $"Executed at: {DateTime.Now}.");
        }


This approach is pretty useful in the sens that can provide a safer construction of low level methods like logs, for example, simplifying the way they will be called, without the necessity to add property names in hardcore.


Asynchronous Methods

The important keywords async and await allowed us to construct methods in asynchronous mode in an easier way than before. It followed the principles of Task-based Asynchronous Pattern.

Now, the methods written using this keywords will return a Task object, that it is a representation of the concept known as Promise Model of Concurrence, originated from functional programming, and now broadly used for distributed systems architecture and construction.

This example shows the behavior of an asynchronous method that doesn't return results: 

        
        // asynchronous methods now defined as async keyword
        private static async void CallingVoidAsyncMethod()
        {
            //the presence of await keyword ensures that this method will be executed in asynchronous mode
            await DBProcess();
        }

        private static Task DBProcess()
        {
            return Task.Run(() =>
            {
                //execute some process
                System.Threading.Thread.Sleep(10000);
            });
        }


On the other hand, this example calls an external API in an asynchronous way, return the result after this:

        
        public async Task GetRepositoryDetails()
        {
            HttpClient client = new HttpClient();
            Uri address = new Uri("https://api.github.com/repos/fabioono25/");
            client.BaseAddress = address;

            HttpResponseMessage response = await client.GetAsync("csharpstudy");

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();

                return JsonConvert.DeserializeObject(result);
            }
            else
            {
                return null;
            }
        }


The benefits of this technique are clear, with non-blocking programming approach and a thread management in an easier way, helping us as developers a lot during the developmment.

---------------------------------------

I hope you have enjoyed this journey until now. Have a nice week!

Here is the entire list of this series:




Fabio Ono

No comments:

Post a Comment