Monday, August 17, 2015

Some thoughts on using technology

      This is one of  two formula shared on the bloggers.Still I am not sure whether this post is really helpful to you, I continue to hope/think someone somewhere might find them useful.
      As per studies, Overall humans development happens when you are not at work or not doing productive work for example, 1.Having your food  2. Meeting your friends( of course ,not social media ) 3.Spending time with family 4.Rest Rooms(Yes , It is true) ....etc. The time spend on these are very critical to overall development of humans in general . But the reality is whenever we get time like this ,we hook to smartphones , e-Readers/Books,Social Media, Selfies
      Keeping these things in mind, I came with my way of handling these situations    
1. Social Media :My social media visits are scheduled(Once in a week), I do not care how many likes i got for my update.
2.Emails :I check and reply mails on Pre-scheduled  basis( twice in a week day)
3.Smartphone/Gadgets: I use these things on problem-solution /need specific.If a gadgets( like smartphone) helps me to save time, I`ll use it.
4.Internet/News/Google : This has to be pre determined purpose specific, Suppose I  need some information on specific topic.i use Internet to get it and close the browser,I do not involve myself into the web of links(I am trying hard to follow this suggestion )
       In nutshell, The technology should help us in doing the stuffs faster, saves times, We should not become salves to technology. Go out and enjoy your time with family/friends

My personal formula before buying anything,quite literally anything

     I was not sure whether this post is helpful for anyone  but still published this post thinking someone, somewhere might actually find it helpful .I have been using this 'formula' before buying anything since my childhood ,I do not remember from where i learnt this great 'formula'

     Generally, we buy items based on the wrong assumption such as

  • Many of friends/peers are having it and i should also get it ( i call this as "Peer Pressure")
  • Many of items we bought , we do not need it/ use it ( I call this as "Robbery" , Yes it is, at-least according to MG  )
  • Just for false prestige ( One of most dangerous superstitions in the world) 
  •  Feel to get more importance among your friends /peers
  • And It continues 

My personal 4 step formula for buying anything. In fact you need to ask yourself these 4 questions

  1. you need to decide  that you should actually need that item, I mean to say you should ask yourself whether you really need it ?
  2. Is that item going to change your life after you bought it ?
  3. Are you going to feel really great after your bought it ?
  4. Are you going actually to use it  ?
If you have answered at least 2 as "YES", you should go ahead and buy it.From my experience, It takes hardly a minute to decide and have been seeing  my expenses under my control. you must give a try for a month or two and see how it woks. if it does not not work for you ,Dump this formula in the dustbin

**Statutory Warning** : I did not consider your budget, Because I`m confident you all can afford it whatever you want in your life , Come on, you have EMIs to get them

Monday, August 10, 2015

The C# dynamic Keyword in C# 4.0 and upwards

dynamic keyword a specialized form of System.Object, in that any value can be assigned to a  dynamic data type.
• a data point declared with the dynamic keyword can be assigned any initial value at all, and can be reassigned to any new (and possibly unrelated) value during its lifetime.
• IntelliSense is not possible with dynamic data.
• It is not mandatorily to use dynamic keyword  (With Dynamic keyword, The intention is to reduce verbose, that’s it)
• It is not statically typed it means that validity of dynamic statement is checked at runtime not at compile time
• Invalid dynamic statement is validated at run time using RuntimeBinderException  available in Microsoft.CSharp.dll
• Cannot make use of lambda /anonymous methods
• Cannot understand any extension method
• It is used in cases where your .Net application interacts with COM libraries (like Microsoft Office products) or VSTO application
• Can be used as parameters, return value or a member of class/structure


Extract Method Refactoring in C#

When to use this:
  1. Length of the method is not an issue
  2. To improve clarity of the code
  3. The name of the new method will reveal the intention of the code in a better way. If you can't come up with a more meaningful name, don't extract the code
       Example : Suppose we have method to use this extract method. by selecting console.WriteLine statement as below

        public void Add(int x, int y)
        {
            int z = x + y;
            Console.WriteLine(z);

        }

We can also right-click the selected code, point to Refactor, and then click Extract Method (Short cut  is CTRL+R, M)  to display the Extract Method dialog box.

The final extracted method is as follows

Monday, August 3, 2015

Asynchronous in C#

       The async keyword of C# is used to qualify that a method, lambda expression, or anonymous method should be called in an asynchronous manner automatically. For this, the CLR will create a new thread of execution to handle the task at hand. Furthermore, when you are calling an async method, the await keyword will automatically pause the current thread from any further activity until the task is complete, leaving the calling thread free to continue on its merry way.

1.Methods (as well as lambda expressions or anonymous methods) can be marked with the async keyword to enable the method to do work in a non-blocking manner e.g. in the below figure, The method ProcessSleep() is marked with async
2.Methods (as well as lambda expressions or anonymous methods) marked with the
async keyword will run in a blocking manner until the await keyword is encountered. e.g. in the above figure, The method will run in a blocking manner until the await sleepingClass.DoSleep() statement is encountered
3.A single async method can have multiple await contexts. e.g.  the above method can multiple await calls
4.When the await expression is encountered, the calling thread is suspended until the awaited task is complete. In the meantime, control is returned to the caller of the method. e.g. In the above method ProccessSleep(), When await expression is encountered, The calling thread i.e. The main method thread is suspended  until the awaited task is complete.In meantime, Control is returned to the Main method to continue executing its statements
5.The await keyword will hide the returned Task object from view, appearing to directly return the underlying return value. Methods with no return value simply return void.
6.As a naming convention, methods that are to be called asynchronously should be marked with the “Async” suffix..e.g. In case, they are SleepingClass.DoSleep() and ProcessSleep()

Thoughts on LINQ

1.LINQ queries look similar to SQL queries, the syntax is not identical. In fact consider LINQ queries as unique statements, which just “happen to look” similar to SQL.

2.The LINQ API provides a consistent, symmetrical manner in which programmers can obtain and manipulate “data” (it can be DB ,XML ,collection, Generics.. etc.)

4.System.Array does not directly implement the IEnumerable<T> interface, it indirectly gains the required functionality of this type (as well as many other LINQ-centric members) via the static System.Linq.Enumerable class type.

5.Deferred Execution :LINQ query expressions is that they are not actually evaluated until you iterate over the sequence            

5.Immediate Execution :When you need to evaluate a LINQ expression from outside the confines of foreach logic, you are able to call any number of extension methods defined by the Enumerable type as ToArray<T>(),ToDictionary<TSource,TKey>(), and ToList<T>(). These methods will cause a LINQ query to execute at the exact moment you call them, to obtain a snapshot of the data

6.For Non generic collection, it is still possible to iterate over data using the generic Enumerable.OfType<T>() extension method.

7.Forms of LINQ :
var result = from matchingItem in container select matchingItem;

var result = from item in container where BooleanExpression select item;

Monday, July 27, 2015

Delagtes and Events concept in OOPs

You will get many example on how to use delegates and events.In this post,I`ll try to explain about delegates  and event concept in C#                                                                                                     
    In the above example The client code(any application) consumes service code(it can be any code/application which provide some kind of processing/service to the client) by object construction or any other means.
   The client code can call methods from the service code based on the domain context but how does service code can call methods from the client code .Essentially,They are Callback requirements.In C#, We have "Delegate" and "event"  concept to implement this kinds of callbacks
  With Delegate, The client code will get full flexibility of invoking itself and Event provide  a limited flexibility to the client code.

Key Points:

  1. This client code is called subscriber to this service code
  2. There can be many subscribers
  3. The methods(in client code)  which are to be called from service code are called Callbacks/Event Handler
  4. It is common practice to use private delegate or instead use Events
  5. There can  be many callbacks withing same client code or different client code
  6. In C# , delegates are all type safe , It means that, All subscriber should provide callbacks which has same signatures with that of delegate defined on the service side