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

Wednesday, 29 January 2014

Difference between Types and Type Members in C#

                                                                                                                                           Previous....
                                                                                                                                                   Next.....



In the example below Customer is the Type and private fields(_id, _firstName, _lastName), Properties(Id, FirstName, LastName) and GetFullName() method are type members.


public class Customer
{    
 #region Private Fields    
 private int _id;    
 private string _firstName;    
 private string _lastName;    
 #endregion    
 #region Properties    
 public int Id   
  {       
 get
 {
 return _id;
 }      
  set
 {
 _id = value;
 }    
 }    
 public string FirstName   
  {       
  get return _firstName;
 }         
set
 { _firstName = value;
 }   
  }  
   public string LastName   
  {       
  get
 {
 return _lastName;
 }         
set
 
_lastName = value;
 }    
 }    #endregion     
#region Methods  
public string GetFullName()   
  {       
  return this._firstName + " " + this._lastName;    
 }    
 #endregion
}
                                                                                                                                           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...