Asp.Net Session State Management

I have gone through wonderful information regarding session state mangement. Posting here for your info.
http://www.codeproject.com/KB/aspnet/ExploringSession.aspx

Advertisements

Anonymous Types & Implicitly Typed Variables:

While writing LINQ queries, there might be situation to return information from several classes. However, when you retrieve information from different class sources in this manner, you cannot retrieve a generic list of your class type because you are not retrieving a specific class type. This is where Anonymous Types step in and make things easier because Anonymous Types allow you to create a class structure on the fly.

When you create an Anonymous Type you need to declare a variable to refer to the object. Since you do not know what type you will be getting (since it is a new and anonymous type), you can declare the variable with the var keyword. This technique is called using an Implicitly Typed Variable.

When writing a LINQ query expression, you may return various pieces of information. You could return all of these data bits and create an Anonymous Type to store them. For example, let’s assume you have a List and each Customer has a BusinessAddress property of type Address. In this situation you want to return the CompanyName and the State where the company is located.

Extension Methods:

This new C# 3.0 feature enables us to add new static methods to existing types with out touching the source files of those types. As per the code we can declare extension methods as

  • Declare the static class to hold those methods.
  • Add the method definition, while declaration add ‘this’ keyword before which are adding this extension method. i.e. for ex: Add, we added this before int, this means we added this method on int types.
  • In the first two methods, we need to pass one parameter, whereas for the third method no need to pass parameter as this syntax is used to show that this method is invoked on ‘int’ types.

public static  class Methods

    {

        public static int Add(this int a, int b)

        {

            return a + b;

        }

        public static int DoubleTheSum(this int a, int b)

        {

            return(( a.Add(b))*(a.Add(b)));

        }

        public static int Negate(this int a)

        {

            return (-a);

        }

    }

  

    class Program

    {     

        static void Main(string[] args)

        {

            int a = 1;

            int b = 9;

            int c=a.Add(b);

            Console.WriteLine(“Sum : “ + c);

            Console.WriteLine(“Double of Sum : “ + a.DoubleTheSum(b));

            Console.WriteLine(“Negation of a is: “ + a.Negate());

            Console.ReadLine();           

           

        }      

    }

 

Object & Collection Initializers :

Object Initializers: C # 3.0 new features which allow direct named initialization of public variables of a class with out need of constructors.This is a great feature as it removes the need to create multiple overloaded constructors using different parameter lists to achieve the same goal. While you can currently create your own constructors, Object initializers are nice because you do not have to create multiple overloaded constructors to handle the various combinations of how you might want to initialize the


class Student

    {

        public int RollNo;

        public string StuName;

        public float Total;

    }

  

    class Program

    {

        static void Main(string[] args)

        {

            Student s1 = new Student { RollNo = 21, StuName = “shalini

            p”, Total = 83.63 };

        }      

    }

 

Collection Initializers: Prior to C# 3.0, it’s not allowed to initialize the collection of objects during initialization itself, i.e. First need to initialize and then need to add objects to the collection variables.

But with this Collection Initialisers, we can initialize the list of objects as


 

    class Student

    {

        public int RollNo;

        public string StuName;

        public float Total;

    }

  

    class Program

    {

        static void Main(string[] args)

        {

            List<Student> sList = new List<Student> { new Student {

            RollNo = 1, StuName = “shalini”, Total = 83 }, new Student

            { RollNo = 2, StuName = “pratyusha”, Total = 82 } };

 

        }      

    }

 

Anonymous Properties:

This is the new feature introduced in C# 3.0

Prior to C# 3.0, we have properties declared as below.

 

  class Test

    {

        private string MyName;

        public string Name

        {

            get

            {

                return MyName;

            }

            set

            {

                MyName = value;

            }

        }

    }

 There is no much logic inside the properties getters and setters; these are made short in C# by name Anonymous Properties as

   class Test

    {

        public string MyName { get; set; }

       

    }

 

Using Properties, we can restrict variable properties like ‘Read-Only (only getter)’, ‘Write only’ (Only setter). Read Write (Both getter & Setter).

I.e. Prior to C# 3.0, we can include either get or set or both.

But Anonymous Properties as a new feature in C# 3.0, we need to include both get and set while declaring the properties. But we can achieve the ‘Read-Only’ by declaring as

  class Test

    {

        public string MyName { get;private set; }

       

    }

||’ly we can achieve ‘Write Only’ property by declaring as

    class Test

    {

        public string MyName { private get; set; }

       

    }

If the property has both getter & setter of public type, one can think why to go for properties instead of public variables.

Here is a lay down of differences between variables & properties:

  • Variables have single line of declaration & single of piece of memory allocated for them. Whereas for properties, there are series of declarative statements and memory will be allocated for the whole block.
  • Custom manipulations cannot be done with variables, whereas properties can be done using setters and getters.
  • Unlike a variable, the value of a property might not correspond directly to a single item of storage. The storage might be split into pieces for convenience or security, or the value might be stored in an encrypted form. In these cases the Get procedure would assemble the pieces or decrypt the stored value, and the Set procedure would encrypt the new value or split it into the constituent storage. A property value might be ephemeral, like time of day, in which case the Get procedure would calculate it on the fly each time you access the property.