Tuesday, April 21, 2015

Abstract class in C#

Abstract class is one of the most discussed things in C#. But for beginner they always found difficulties to understand it. They quite have a good reasons for that. The main question which every one tries to crumbling is why do we need an abstract class. We have interface to specifies signature. Well' if you are thinking in this direction you are right. Before going deep down to abstract class lets do some basic.

As per MSDN "The abstract keyword enables us to create classes and class members that are incomplete and must be implemented in a derived class". The golden rules of abstract class are 


1. The keyword abstract is used to specify class, method, property, indexer, and event as abstract.

2. If a class is specify as abstract it must be public or internal.

3. If a method, property, indexer, and event is declared as abstract then the class should also be declared as abstract.

4. We cannot create an object of a abstract class. Abstract class can only be inherited.


Example


    public abstract class BasicPay
    {
        public int FixConponent=10000;
        public abstract double GetBasic(int Basic);

        public double GetSalary(int Basic)
        {
            return GetBasic(Basic) + FixConponent;
        }
    }


In this example the class BasicPay is declared as abstract. It has two methods. GetBasic and get GetSalary. Here GetSalary has its body and declaration, because the business logic of behind this method will be same across all sub classes. But GetBasic method is declared as abstract because it will be different on different sub classes. Abstract method can be override in sub classes using override keyword. The below code snippet show how inherit and abstract class.



    public class SalesSalary : BasicPay
    {
        public override double GetBasic(int Basic)
        {
            //do some logic.
            return Basic * 5;
        }
    }
    public class ITSalary : BasicPay
    {
        public override double GetBasic(int Basic)
        {
            //do some logic.
            return Basic * 7;
        }
    }


This example clarify the objective of an abstract class. This is the one and only reason that we need and abstract class over interface. In short we need an abstract class where signatures are different for business logic and also some implementations required for consistency through out the sub classes. Here GetBasic is a signature and has different meaning for different sub classes and GetSalary is consistent across all sub classes.


Complete program


using System;

namespace Tutorials_AbstractClass
{
    //abstract class
    public abstract class BasicPay
    {
        public int FixConponent=10000;
        public abstract double GetBasic(int Basic);

        public double GetSalary(int Basic)
        {
            return GetBasic(Basic) + FixConponent;
        }
    }

    public class SalesSalary : BasicPay
    {
        public override double GetBasic(int Basic)
        {
            //do some logic.
            return Basic * 5;
        }
    }
    public class ITSalary : BasicPay
    {
        public override double GetBasic(int Basic)
        {
            //do some logic.
            return Basic * 7;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // creating object of derived class
            SalesSalary ss= new SalesSalary();
            ITSalary it = new ITSalary();

            Console.Clear();
            Console.WriteLine("Enter basic salary :");
            int BasicSalary = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Salary for IT : {0}",it.GetSalary(BasicSalary));
            Console.WriteLine("Salary for Sales : {0}", ss.GetSalary(BasicSalary));
            Console.ReadLine();
        }
    }
}

Output

Enter basic salary :       
3000                           
Salary for IT : 31000    
Salary for Sales : 25000


Happy Coding...)

Monday, April 20, 2015

Interface in C#

An interface looks like a class, but has no implementation. It can be a member of a namespace or a class and can contain signatures of methods, properties, events or indexers.That's why it is said to be less concrete, more abstract.At the time of execution, the runtime selects an implementation of the interface.

An interface can be created using the keyword interface.

Syntax

<access modifier> interface <interface name> 
{                                                                   
 //declare member                                          
}                                                                   

Access modifier of an interface is public or internal. By default it is internal. Hence if you don't mentioned any access modifier then it is considered as internal. But members of an interface are always public. You need not to prefix any access modifier for them.


    
    public interface IExample
    {
        //member property
        int BasicPay { get; set; }
        //.....

        //member function
        double GrossSalary();
    }

Implementing an interface is itself an art. By choosing the efficient way we can avoid the classic diamond problem. Or we can say C# remove this problem by design different way of interface implementation. There are two ways of implement an interface. Implicit and explicit.

Implicit implementation


    
    public class Example : IExample
    {
        private int _BasicPay;

        public int BasicPay
        {
            get
            {
                return _BasicPay;
            }
            set
            {
                _BasicPay = value;
            }
        }

        public double GrossSalary()
        {
            //some bussiness logic
            return _BasicPay * 4.5;
        }
    }

Explicit implementation

A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.


    public class Example : IExample
    {
        private int _BasicPay;

        int IExample.BasicPay
        {
            get
            {
                return _BasicPay;
            }
            set
            {
                _BasicPay = value;
            }
        }

        double IExample.GrossSalary()
        {
            //some bussiness logic
            return _BasicPay * 4.5;
        }
    }

Complete program

using System;

namespace InterfaceExample
{
    public interface IExample
    {
        //member property
        int BasicPay { get; set; }
        //.....

        //member function
        double GrossSalary();
    }


    public class Example : IExample
    {
        private int _BasicPay;

        int IExample.BasicPay
        {
            get
            {
                return _BasicPay;
            }
            set
            {
                _BasicPay = value;
            }
        }

        double IExample.GrossSalary()
        {
            //some bussiness logic
            return _BasicPay * 4.5;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.Clear();
            IExample example = new Example();
            Console.WriteLine("Enter basic salary : ");
            example.BasicPay = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Gross salary is : {0}", example.GrossSalary().ToString());
            Console.ReadLine();
        }
    }

}

Output

Enter basic salary :       
112000                        
Gross salary is : 504000


Happy Coding ..:)

Tuesday, April 7, 2015

Install ASP.NET 4.5 into Windows Server 2012

Last week I was working on hosting an ASP.NET site in Windows Server 2012. As this was my first time to work with Windows Server 2012 I faced several hurdles. Somehow I managed to install IIS on it. There are tons of articles online to help me out. I found "http://www.iis.net/learn/get-started/whats-new-in-iis-8/installing-iis-8-on-windows-server-2012" this one was very helpful. Here I am not going to discuss how to install IIS. Hope the above article helps you too.

After installing IIS I configured my site. But it is not running, throwing some handlers error. Oh crap. I need to register ASP.NET into IIS. But irony was that “aspnet_regiis –i” not working. It throws an error.

"'aspnet_regiis' is not recognized as an internal or external command, operable program or batch file."


After couple of hours of searching I found aspnet_regiis is no more supported on Windows Server 2012. Microsoft introduced and new tool called DISM. Deployment Image Servicing and Management.

DISM can deploy services into your operating system from a Windows Image or from internet.

Syntax for DISM (for my purpose)

DISM.exe {/image:<path_to_image> | /online} [dism_options] {servicing_command} [<servicing_argument>]

{/image:<path_to_image> | /online}
This is the first parameter. It gives us an option from where to install the service from and Windows image or from online.


[dism_options]
Second parameter specifies what to do. Ex. enable-feature


{servicing_command}
Tell DISM what to install


My asp.net registration into IIS turned out as

dism /online /enable-feature /featurename:IIS-ASPNET45 /all

Here /all tell DISM to install all dependency of ASPNET45.


 Finally my ASP.NET get installed on Windows Server 2012.

Happy Coding.. :)