Friday 7 February 2014

Polymorphism in C#

                                                                                                                                                Previous....

                                                                                                                                                     Next.....


The word polymorphism means having more then one forms. 
Polymorphism can be static or dynamic. In static polymorphism it is determined at the compile time. In dynamic polymorphism , it is decided at run-time.

Static Polymorphism/Function Overloading

It is an compile time polymorphism  because compiler already knows what type object it is linking to and waht type of method going to call. So linking a method at compiler time also called as early binding.
"In this Code method name same but its parameters are different  so it is called operate overloading."
using System;
namespace Polymorphism
{
   class calculation 
   {
      public int add(int a, int b)
      {
         return(a+b);
      }

      public int add(int a, int b,int c)

      {
         return(a+b+c);

      }

     }
Dynamic Polymorphism
It is an Run time polymorphism  because it is link at run time. So linking a method at run time also called as late binding.
"In this Code method name same and its parameters are also same  so it is called operate overriding."

In base class the method is declare "virtual" and in the derived class we override the same method. the virtual keyword indicate the method can be overridden in any derived class.
During run time, method overriding can be achieve by using inheritance principle and using "virtual" and "override" keyword.    
using System;
namespace employee
{
   public string firstname = "FN";
   public string secondname = "SN";

      public virtual void printfullname()
   console.writeline(firstname + " " + lastname);         }}

   public class parttime employee : employee
   {
      public override void printfullname()
      console.writeline("firstname + " " lastname + " parttime");  }}

      public class fulltimeemployee : employee
      {
         public override void printfullname()
        {
      console.writeline("firstname + " " lastname + " fullname");  }}
public class temporaryemployee : employee 
      { public override printfullname()
      { 
        console.writeline("firstname + " " lastname + " temporary ");  }}

         public class programme
      { public static void main()
   employee[] emp = new employee[4];
   emp [0] = new employee();
   emp [1] = new parttimeemployee();
   emp [2] = new fulltimeemployee();
   emp [3] = new tomporarytime employee();
   
     foreach(employee e in emp)

       {
         e.printfullname();
         
      }
   }
}
When the above code is compiled and executed, it produces the following result:
FN SN ,FN SN parttime

FN SN fulltime,FN SN temporary 
                                                                                                                                                                                              Previous....
                                                                                                                                                     Next.....

Inheritance in C#

                                                                                                                                                Previous....

                                                                                                                                                     Next.....


One of the most important concepts in OOPS  is  inheritance. Inheritance allows us to define a class in terms of another class, This also provides an opportunity to reuse the code functionality .
In inheritance a class inherit the property of other class. This existing class is called the base class, and the new class is referred to as the derived class.
Base and Derived Classes
A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base class.
 syntax 
<acess-specifier> class <base_class>
{
 data
}
class <derived_class> : <base_class>
{
 data
}
Consider a base class Shape and its derived class Rectangle:
using System;
namespace Inheritance
{
   class Add
   {
      public void number1(int n)
      {
         number1 = n;
      }
      public void number2(int m)
      {
         number2 = m;
      }
      protected int number1;
      protected int number2;
   }

   // Derived class
   class divident: Add
   {
      public int devide()
      { 
         return (number1 / number2); 
      }
   }
   
   class calculator 
   {
      static void Main(string[] args)
      {
         calculator cal = new calculator();

         cal.number1(8);
         cal.number(2);
Console.WriteLine("the number: {0}",  cal.devide());
         Console.Readline();
      }
   }
}
When the above code is compiled and executed, it produces the following result:
the number : 4

Base Class Initialization

The derived class inherits the base class member variables and member methods. Therefore thesuper class object should be created before the subclass is created. You can give instructions for superclass initialization in the member initialization list.
using System;
namespace inheritance
{
   class calculator
   {
      //member variables
      protected double number1;
      protected double number2;
      public calculator(double n1, double n2)
      {
         number1= n1;
         number2 = n2;
      }
      public double product()
      {
         return number2number2;
      }
      public void Display()
      {
         Console.WriteLine("number1: {0}", number1);
         Console.WriteLine("number2: {0}", number2);
         Console.WriteLine("product: {0}", product());
      }
   } 
   class Tabletop : calculator
   {
      private double cost;
      public Tabletop(double n1, double n2) : base(n1, n2)
      { }
      public double GetCost()
      {
         double cost;
         product = product() * 70;
         return product;
      }
      public void Display()
      {
         base.Display();
         Console.WriteLine("product: {0}", product());
      }
   }
   class Executecalculator
   {
      static void Main(string[] args)
      {
         Tabletop t = new Tabletop(4.5, 7.5);
         t.Display();
         Console.ReadLine();
      }
   }
}

Multiple Inheritance in C#

C# does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance. 
using System;
namespace Inheritance
{
   class prod 
   {
      public void number1(int n1)
      {
         number1= n1;
      }
      public void number2(int n2)
      {
         munber2 = n2;
      }
      protected int munber1;
      protected int munber2;
   }


   public interface Cost 
   {
      int Cost(int devide);

   }
   // Derived class
   class Rectangle : product, devide
   {
      public int product()
      {
         return (munber1 * munber2);
      }
      public int Cost(int product)
      {
         return product * 70;
      }
   }
   class calculation
   {
      static void Main(string[] args)
      {
         calculation cal = new calculation();
         int product;
         cal.number1(5);
         cal.number2(7);
         product = cal.product();
         // Print the area of the object.
         Console.WriteLine("product: {0}",  cal.product());
         Console.WriteLine("cost: {0}" , cal.Cost(product));
         Console.Readline();
      }
   }
}
When the above code is compiled and executed, it produces the following result:
product: 35
cost: 2450



                                                                                                                                                Previous....
                                                                                                                                                     Next.....

Tuesday 4 February 2014

Session in Dot Net

                                                                                                                                          Previous...

Q:How Many types of session modes available in asp.net?
Ans: 

  • In-Process – It stores the session in local system.
  • State Server – It stores the session in a process called “ASP.NET state service”.
  • SQLServer – It stores the session in SQL Server database.
  • Custom – It allows the custom storage provider.
Q:What is the default session modes in asp.net?
Ans: InProcess

Q:What are the disadvantages of using InProc session mode?
Ans: Its stores session information in the current Application Domain.
So it will lose data if we restart the server.

Q:Session_End() event is supported by which session mode only?
Ans: Session_End() event is supported by InProc mode only.

Q:What do you understand by StateServer(Out-Proc) mode?
Ans: StateServer session mode is also called Out-Proc session mode. StateServer uses a stand-alone Windows Service which is independent of IIS and can also be run on a separate server. This session state is totally managed by aspnet_state.exe. This server may run on the same system, but it's outside of the main application domain where yourweb application is running. This means if you restart your ASP.NET process, your session data will still be alive.

Q:Under StateServer(Out-Proc) mode the session state is managed by?
Ans: aspnet_state.exe

Q:What are the advantages and disadvantages of StateServer(Out-Proc) Session mode?
Ans: Advantages:
It keeps data separate from IIS so any issues with IIS will not hamper session data.
It is useful in web farm and web garden scenarios.
Disadvantages:
Process is slow due to serialization and de-serialization.
State Server always needs to be up and running.

Q:Under SQLServer Session Mode where the session data store?
Ans: In SQLServersession mode, session data is serialized and stored in A SQL Server database.

Q:What is the big disadvantage of SqlServer Session mode?
Ans: The main disadvantage of SqlServer Session mode storage method is the overhead related with data serialization and de-serialization.

Q:What are the advantages and disadvantages of SqlServer Session mode?
Ans: Advantages:
Session data not affected if we restart IIS.
The most reliable and secure session management.
It keeps data located centrally, is easily accessible from other applications.
Very useful in web farms and web garden scenarios.
Disadvantages:
Processing is very slow in nature.
Object serialization and de-serialization creates overhead for the application.
As the session data is handled in a different server, we have to take care of SQL Server. It should be always up and running.



                                                                                                                                          Previous...


C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...