Sunday, 19 July 2015

C# equivalent of VB's With keyword



There’s no with keyword in C#, like Visual Basic. So you end up writing code like this:
this.StatusProgressBar.IsIndeterminate = false;
this.StatusProgressBar.Visibility = Visibility.Visible;
this.StatusProgressBar.Minimum = 0;
this.StatusProgressBar.Maximum = 100;
this.StatusProgressBar.Value = percentage;

Here’s a workaround to this:
this.StatusProgressBar.Use(p =>
{
  p.IsIndeterminate = false;
  p.Visibility = Visibility.Visible;
  p.Minimum = 0;
  p.Maximum = 100;
  p.Value = percentage;
});

This saves you repeatedly typing the same class instance or control name over and over again. It also makes code more readable since it clearly says that you are working with a progress bar control within the block. It you are setting properties of several controls one after another, it’s easier to read such code this way since you will have dedicated block for each control.
It’s a very simple one line extension method that enables it:
public static void Use<T>(this T item, Action<T> work)
{
    work(item);
}

You could argue that you can just do this:
var p = this.StatusProgressBar;
p.IsIndeterminate = false;
p.Visibility = Visibility.Visible;
p.Minimum = 0;
p.Maximum = 100;
p.Value = percentage;

But it’s not elegant. You are introducing a variable “p” in the local scope of the whole function. This goes against naming conventions. Morever, you can’t limit the scope of “p” within a certain place in the function.
Ex:
static class Extension
{
    public static void With<T>(this T obj, Action<T> a)
    {
        a(obj);
    }    
}
 
class Program
{
    class Obj
    {
        public int Prop1 { get; set; }
        public int Prop2 { get; set; }
        public int Prop3 { get; set; }
        public int Prop4 { get; set; }
    }
 
    static void Main(string[] args)
    {
        var detailedName = new Obj();
        detailedName.With(o => {
            o.Prop1 = 1;
            o.Prop2 = 2;
            o.Prop3 = 3;
            o.Prop4 = 4;
        });
    }
}




Difference between Static and Singleton classes



1)    A singleton classes allowed create only a single instance of particular class. That instance can be treated as normal object. You can pass that object to a method as parameter or you can call the class method with that Singleton object.
While static class can have only static methods and you cannot pass static class as parameter.
2)    We can implement the interfaces with the Singleton class while we cannot implement the interfaces with static classes.
3)    We can clone the object of Singleton classes; we cannot clone the object of static classes.
4)    Singleton objects stored on heap while static class stored in stack.
5)    A Singleton class can extend the classes (support inheritance) while static class cannot inherit classes.
6)    Singleton class can initialize lazy way while static class initialize when it loaded first.
7)    Static classes are sealed class while Singleton classes are not sealed.