Friday, 7 February 2014

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...


Saturday, 1 February 2014

Namespace in C#

                                                                                                                                                                                  Previous.......
                                                                                                                                                                                              Next......
namespace are used to organised your programme. they provide assistance in avoiding name classes.

Defining a Namespace


namespace namespace_name
{
   // code 
}
To call the namespace-enabled version of either function or variable the namespace name as follows:
namespace_name.item_name;
The following program demonstrates use of namespaces:
using System;
using xyz

namespace First
{
    using M;

    class Program
    {
 static void Main()
 {
     // Can access CClass type directly from xyz
     zClass class1 = new CClass();

     // Can access DClass type from M.
     MClass class2 = new DClass();

     // Must explicitely specify F namespace.
     F.FClass class3 = new F.FClass();

     // Display types.
     Console.WriteLine(class1);
     Console.WriteLine(class2);
     Console.WriteLine(class3);
 }
    }
}

namespace x
{
    namespace y
    {
 namespace z
 {
     public class zClass
     {
     }
 }
    }
}

namespace M
{
    public class MClass
    {
    }
}

namespace F
{
    public class FClass
    {
    }
}
OUTPUT :-
XYZ.ZClass
M.MClass
F.FClass

                                                                                                                                        Previous.......

                                                                                                                                                                                              Next......

Access modifier in C#

                                                                                                                Previous...
                                                                                                                        Next..


Access modifiers are an integral part of object-oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn't have access to certain features.

In C# there are 5 different types of Access Modifiers.
Modifier
Description
public
There are no restrictions on accessing public members.
private
Access is limited to within the class definition. This is the default access modifier type if none is formally specified
protected
Access is limited to within the class definition and any class that inherits from the class
internal
Access is limited exclusively to classes defined within the current project assembly
protected internal
Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.


Public Modifiers: 
The public keyword is an access modifier for types and type members. Public access is the most permissive access level.

There are no restrictions on accessing public members.

Accessibility: 
  • Can be accessed by objects of the class
  • Can be accessed by derived classes
Example: In the following example num1 is direct access.

using System;
namespace AccessModifiers
{
    class Program
    {
        class AccessMod
        {
            public int num1;
        }
        static void Main(string[] args)
        {
            AccessMod ob1 = new AccessMod();
            //Direct access to public members
            ob1.num1 = 100;
            Console.WriteLine("Number one value in main {0}", ob1.num1);
            Console.ReadLine();
        }
    }
}
Private Modifiers:

Private access is the least permissive access level.

Private members are accessible only within the body of the class or the struct in which they are declared.

Accessibility: 
  • Cannot be accessed by object
  • Cannot be accessed by derived classes
Example: In the following example num2 is not accessible outside the class.

using System;
namespace AccessModifiers
{
    class Program
    {
        class AccessMod
        {
            public int num1;
            int num2;
        }
        static void Main(string[] args)
        {
            AccessMod ob1 = new AccessMod();
            //Direct access to public members
            ob1.num1 = 100;
            //Access to private member is not permitted
            ob1.num2 = 20;
            Console.WriteLine("Number one value in main {0}", ob1.num1);
            Console.ReadLine();
        }
    }
}



The above program will give compilation error, as access to private is not permissible. In the below figure you can see the private member num2 is not available.

private.jpg

Protected Modifiers:
A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.

Accessibility: 
  • Cannot be accessed by object
  • By derived classes
using System;
namespace AccessModifiers
{
    class Program
    {
        class Base
        {
            protected int num1;
        }
        class Derived : Base
        {
            public int num2;
            static void Main(string[] args)
            {
                Base ob1 = new Base();
                Derived ob2 = new Derived();
                ob2.num1 = 20;
                // Access to protected member as it is inhertited by the Derived class
                ob2.num2 = 90;
                Console.WriteLine("Number2 value {0}", ob2.num2);
                Console.WriteLine("Number1 value which is protected {0}", ob2.num1);
                Console.ReadLine();
            }
        }
    }
}



In the above program we try to access protected member in main it is not available as shown in the picture below that num1 is not listed in intellisense.

protected.jpg

internal modifier 

The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll).

In other words, access is limited exclusively to classes defined within the current project assembly.

Accessibility:

In same assembly (public) 
  • Can be accessed by objects of the class
  • Can be accessed by derived classes
In other assembly (internal) 
  • Cannot be accessed by object
  • Cannot be accessed by derived classes
protected internal 

The protected internal accessibility means protected OR internal, not protected AND internal.

In other words, a protected internal member is accessible from any class in the same assembly, including derived classes.

The protected internal access modifier seems to be a confusing but is a union of protected and internal in terms of providing access but not restricting. It allows:  
  • Inherited types, even though they belong to a different assembly, have access to the protected internal members.
  • Types that reside in the same assembly, even if they are not derived from the type, also have access to the protected internal members.

                                                                                                                Previous...
                                                                                                                        Next..

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...