Memory dumps of a developer

Articles and tutorials on .NET Core, ASP.NET MVC, Kendo UI, Windows 10, Windows Mobile, Orchard

  • Using static statement in C# 6.0

    The using static is another neat little feature introduced in C# 6.0 which helps you to make your code more readable as well as helps to avoid some redundancy in your code.

    We all know that we can import a namespace with using which helps us to use the type name without using the fully qualified name. For example, in most of the programs the first statement will be using System and if we want to call the WriteLine method using the statement given below,

     

    using System;

    public class Program
    {
        public static void Main()

        {

            Console.WriteLine("This is a test");

         }

    }

    Suppose if we didn't imported the namepsace then, instead of Console.WriteLine() we need to call the method with the fully qualified name. ie System.Console.WriteLine() 

    public class Program
    {
        public static void Main()

        {

            System.Console.WriteLine("This is a test");

         }

    }

    Imagine a class with a large number of calls to the WriteLine method and we will be having a lot of redundant 

    code for the owner class, in this case System

    Let's take the below code as an example


  • nameof Expression in C# 6.0

    This function is one of the useful addtion to the language in C# 6.0 and will see a widespread adoption. We as developers have written many times the below code numerous times in our life.

    In this basically we are checking whether the parameters are properly set according the business rules and if it's not then throws the ArgumentNull exception with the message saying that this particular property is not set.

    Suppose after some time we decided to change the name of the properties, went ahead with using the refactoring options available in Visual Studio. In the example I removed the term Product from all the properties using the refactoring option, but the messages for the ArgumentNull exception won't change since it's inside string literals.

    Now when the exception occurs there is a mismatch between the actual property name and the name in the message. Imagine if this occurs in a class containing a lot of properties then it take some time to make the corrections and also there's a possiblity of missing out on some.


  • Auto Properties in C# 6.0

    C# 6.0 brings the ability to initialize an auto-properties pretty much like the way we do for a field. This helps you to intialize the backing field without woking through the setter of the auto property or making using of a local variable. It's executed in the order as written and not at the time when the object is initialized. It means that we cannot reference it using this keyword since the property iniitialization happens before the object intialization.

    In the earlier versions, suppose if we want to initialize the properties to some default values we were doing that at the time of instantiating an object as shown below

    In C# 6.0, now it's possible to initialize the values for the properties at the time of declaration itself.

    Many of you may be wondering why I am using a $ symbol in front of the string and how can I use the C# syntax inside the string which is later transformed into the correct message during execution of the code. It's the new String interpolation feature in C# 6.0 and you read more about it here.

    And you can mix and match the defaults with the ones you want as shown below. In this example, I have modified the default values at the time of instantiation for FirstName and LastName properties and left the Email property untouched

    Also we can initialize it without a setter also.


  • Dictionary Initializers in C# 6.0

    Dictionary initialization was first introduced with C# 3.0 and the syntax for the same remained same since then.

    In C# 6.0 the syntax was tweaked little bit to make it more friendly to maintain. As we all know in a dictionary object, information is stored as key value pairs and if you look at the above syntax a lot of curly brackets and punctuation marks are used. This makes it pretty ugly and doesn’t reflect the data structure which is holding the data.

    The new syntax reduces the number of keystrokes and the code is now more elegant and readable by omitting some curly braces and the syntax now uses the equals sign which helps you to recognize the info in a key value pair format

    Even though they have introduced a new way on initializing dictionary objects, the old way of writing is still valid and it’s up to you developers to use which one.


  • String Interpolation in C# 6.0

    Up untill now we are using various options for formatting strings using String.Format or by concatenating string values using + operator or String.Concat function. 

    In the case of using + operator , as the number of items that needs to concatenated increases it becomes very hard to maintain and to understand what we are doing and some times it may result in incorrect string ouput too.

    This can be solved to some extent by using the String.Format function by using the literals as placeholders so that we will get to know what the output be in advance by looking at the text part. But here we may find problems if we don't give the parameter values in the same order as in the string text part. Also if the number of placeholders and the values in the parameter list are not in sync an exception will be thrown at runtime.

    In C# 6.0, Microsoft added a new feature to make our life easier by allowing us to put the expressions directly in the string literal. Now we can inject the values into a string using curly bracket enclosed expressions.


  • Null Propagation Operator in C# 6.0

    Every developer who writes C# code should have encountered NullReference exception or Object Reference not set to an instance of an object error at least once. This error normally happens when you try to invoke a member of an object without performing a null check on the object. For example the following code will produce a compiler error

     

    So normally to solve this issue we will introduce a conditional checking before invoking the property as shown below

     


  • Invoking server method from client using SignalR

    In this series I will show you people to how to invoke a method defined in the server side code from javascript and then invoke a method in the client side to show the message returned by the server code. In the earlier post, I have explained in detail the various the steps needed to incorporate SignalR in your project and you read that here if you want to recollect it.


  • Getting started with SignalR

    SignalR is a library from Microsoft which can be used for enabling real-time functionality in your web applications. SignalR uses web sockets feature in HTML5 to maintain a persistent connection between the server and the client. Because of the stateless nature of the web, it was very hard to maintain a persistent connection in the pre HTML5 era and we used techniques such as long polling, meta refresh tag or some timer logic coupled with AJAX calls to achieve it. Please visit their site or in GitHub to read more on the features and documentation, also the entire code is available there.

    SignalR API contains two methods for communication between client and server

    1. Persistent Connections - Represent a single endpoint for sending single-recipient, grouped or broadcast messages. It’s similar to communication based API’s such as WCF.

    2. Hubs - Built upon the Connection API that allows the client and server to call methods on each other directly.Similar to other remote invocation API’s such as .NET Remoting

    Architecture

     

    architecture-diagram

    Picture Courtesy : SignalR Documentation


  • D - Day : Welcome to the Windows Era

    With the clock now past the midnight to ring in July 29th, Microsoft went live with the release of Windows 10 worldwide in 190 countries and in 111 languages. The upgradation will start in each country by  12.00 AM and the rollout process will happen in batches and in a staged manner, so those who reserved their copies don't get worried about the upgrade for the time being, it will be coming to your machines very soon. Also the links for ISO files are also now live, but the catch here is that you will need the serial number during the installation process, for those who have that right now they can proceed with the installation using the iso files. The instructions and links for the iso files can be found here.

    Currently there is a workaround for those who have reserved the copy for Windows 10 and yet to see the update, please find this post in the Windows Central site. I haven't tried this one yet, so proceed with this at your own risk if have ran out of patience. In the meantime you go through these links which has in depth reviews, tip and tricks, help guides etc

    1. Windows 10 FAQ
    2. Windows 10 Official Blog
    3. Windows 10 review by Windows Central
    4. Review by Neowin.net
    5. Windows 10 Quick Guide
    6. Windows 10 Help, Tips & Tricks
    7. Articles by Tech Repository on Windows 10
    8. Nine Important Things in Windows 10 by The Verge

    Watch this space for more on my upgradation experience and till then have fun with the ninja cat


  • D-01 : Windows is Free

    This one statement was the biggest surprise from Microsoft since they announced their plans to release the next version of Windows to the masses. Yes, you heard it right, they are giving it free for all for a year from the date of the launch and anyone wishes to install after that will need to shell out some money. They will be providing support and unlimited access to updates also for those who jumped in the first year too.

    The dwindling sales of the desktops and laptops as well as the poor response to previous version may have forced Microsoft to take this step. But as more and more services and enterprises embracing the cloud and the future plans for Windows as a Service will help Microsoft to gain the lost ground in the long term.

    So do I need to really really upgrade even if the offer is tempting ?

    Windows XP, Vista

    If you are running on Windows XP, then you should really upgrade it. We all know it was one of the best product to come out of Microsoft, but it has reached the end of the life cycle long back and also it was developed prior to Web 2.0 and there are many shortcomings in the OS to run in the modern web era.

    Windows 7

    If you are running on Windows 7, then it will be a difficult option to switch. If you are okay with the OS, then there is no need to upgrade right now. Windows 10 still has some feature from Windows 8.0/8.1 and if you are not going to use any one of them, then stick with the current one. But if you want to exploit the under the hood features of Windows 8 such as faster boot up time, improved task manager, one drive syncing and additional features such as Cortana, Virtual Desktops , then go for it. You won't find much difference between the two softwares and will get you going from the day one itself.

    Windows 8/8.1

    Should upgrade. Windows 10 removes most of the annoying things in Windows 8/8.1 and it's now much more user friendly for non-touch input when compared to the previous version. 

    Also the free upgrade can be done only for the ones having a valid license of Windows XP/7/8. If you are running a pirated version then Microsoft will allow you to upgrade to Windows 10, but still it will be an unlicensed one.