Showing posts with label OOPs. Show all posts
Showing posts with label OOPs. Show all posts

Tuesday, 24 June 2014

When to use abstract class?

What is abstract class?

Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes. Abstract class members marked as abstract must be implemented by derived classes.

When to use abstract class?

The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.


How toCreate use abstract class?

    public abstract class AbsAcType
    {
        public int ACno { get; set; }
        public String ACFirstName { get; set; }
        public String ACLastName { get; set; }
        public decimal Amount { get; set; }
        public decimal InterestRate { get; set; }

        public string CustomerName()
        {
            return ACFirstName + " " + ACLastName;
        }

        public string AcountNo(string BankName)
        {
            return BankName + ACno;
        }

        public abstract decimal AcBalance();

    }



    public class SavingAC : AbsAcType
    {
        public override decimal AcBalance()
        {
            return Amount + InterestRate*(decimal)0.20;
        }
    }

    public class currentAC : AbsAcType
    {
        public override decimal AcBalance()
        {
            return Amount + InterestRate * (decimal)0.028;
        }

    }



        protected void btnSavingAC_Click(object sender, EventArgs e)
        {
            decimal amount,interestRate;
            decimal.TryParse(txtAmount.Text,out amount);
            decimal.TryParse(txtInterestRate.Text,out interestRate);
            string firstName = txtFirstName.Text, lastName = txtLastName.Text;
            AbsAcType objSaving = new SavingAC()
            {
                ACno= 101,
                ACFirstName = firstName,
                ACLastName = lastName,
                Amount=amount,
                InterestRate=interestRate               
            };
            lblAcNo.Text = objSaving.AcountNo("CITI");
            lblFullname.Text= objSaving.CustomerName();
            lblResBal.Text =  objSaving.AcBalance().ToString();
         }

        protected void btncurrentAC_Click(object sender, EventArgs e)
        {
            decimal amount, interestRate;
            decimal.TryParse(txtAmount.Text, out amount);
            decimal.TryParse(txtInterestRate.Text, out interestRate);
            string firstName = txtFirstName.Text, lastName = txtLastName.Text;
            AbsAcType objcurrent = new currentAC()
            {
                ACno = 101,
                ACFirstName = firstName,
                ACLastName = lastName,
                Amount = amount,
                InterestRate = interestRate
            };
            lblAcNo.Text = objcurrent.AcountNo("CITI");
            lblFullname.Text = objcurrent.CustomerName();
            lblResBal.Text = objcurrent.AcBalance().ToString();
        }

Monday, 2 June 2014

Access Modifiers

·    Scope of  Access Modifiers



           public 
-No restrictions to access
-The type or member can be accessed by any other code in the same assembly or another assembly that references it. 
      - We can use this modifier for Class.


·         protected 
-Access is limited to within the class definition and any class that inherits from the class.
-The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
      - Class did not support this access modifier.




·         internal 
-Access is limited exclusively to classes defined within the current project assembly.
-The type or member can be accessed only by code in the same class or struct. 
      - Class default access modifier is internal.  

·         private 
-Access is limited to within the class definition; This is the default access modifier type if none is formally specified.
-The type or member can be accessed by any code in the same assembly, but not from another assembly.
     -Class did not support this access modifier.

·         protected internal
 - The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. 
- Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
- Class did not support this access modifier.

Class in C#


  •  A class is a reference type. It is stored in heap memory.
  •  It is called Blue print.
  •  The default access modifier for a class type is internal
  • A class can only be marked public and internal
  • If we will try to use other modifier it will throw error.




Friday, 23 May 2014

Extension Method

An extension method is a special kind of static method that allows you to add new methods to existing types without creating derived types.
The extension methods are called as if they were instance methods from the extended type, For example: x is an object from int class and we called it as an instance method in int class.
It represents static methods as instance methods. An extension method uses the this-keyword in its parameter list.
It must be located in a static class and static method.


How to Create my Extension Method?
Simply, you create your own static method in your class and put this keyword in front of the first parameter in this method (the type that will be extended).

namespace LooslyCoupled
{
    public static class MyMathExtension
    {
        public static int? NullCheck(this int? x)
        {
            return x == null ? 0 : x;
        }
    }
}


namespace LooslyCoupled
{
    class Program
    {
        static void Main(string[] args)
        {
            int? a = 100;
            int? b=a.NullCheck();
            Console.WriteLine("Value A = "+ a.NullCheck());
            Console.WriteLine("Value B = " + b);
            a = null;
            Console.WriteLine("Value A is null  = " + a.NullCheck());
            Console.Read();
       }
    }
}


General Tips in Extension Methods Usage

This section is optional reading section for understanding the core idea of extension methods:
a.       This keyword has to be the first parameter in the extension method parameter list.
b.       Extension methods are used extensively in C# 3.0 and further version specially for LINQ extensions in C#, for example:

  int[] aa = { 1, 4, 5, 3, 2, 6 };
  var result = aa.OrderBy(g => g);
Here, order by is a sample for extension method from an instance of IEnumerable interface, the expression inside the parenthesis is a lambda expression.
c.       It is important to note that extension methods can't access the private methods in the extended type.
d.       If you want to add new methods to a type, you don't have the source code for it, the ideal solution is to use and implement extension methods of that type.
e.       If you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never be called.