Lamda Expression usage in LinQ

int[] marks = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100 };


int firstClassCount = marks.Where(n => n > 60).Count();

int[] distMarks = marks.Where(n => n > 80).ToArray();

Response.Write("Total first classes : "+firstClassCount.ToString() + "</br>");

foreach (int mark in distMarks)

{

Response.Write("Above Distinction : " + mark.ToString() + "</br>");

}

Lambda Expressions C# 3.0 & Anonymous Methods

Lambda expressions provide a concise syntax for writing anonymous
methods. The C# 3.0 specification describes lambda expressions as a
super set of anonymous methods.


In C# 2.0, you can write a delegate using an anonymous method, as
shown in this example:

public delegate int MyDelegate(int n);

class MyClass
{
    static void Main()
    {
        // Anonymous method that returns the argument multiplied by 5:
        MyDelegate delegObject1 = new MyDelegate(
        delegate(int n) { return n * 5; }
        );
        // Display the result:
        Console.WriteLine("The value is: {0}", delegObject1(5));
    }
}

This program outputs the value 25.

Using a lambda expression you can use a simpler syntax to achieve the
same goal:

MyDelegate delegObject2 = (int n) => n * 5;

using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;

namespace Lambda
{
    public delegate int MyDelegate(int n);
    class MyClass
    {
        static void Main()
        {
            MyDelegate delegObject1 = new MyDelegate(
            delegate(int n) { return n * 5; }
            );

            Console.WriteLine("The value using an anonymous method is: {0}",
            delegObject1(5));

            MyDelegate delegObject2 = (int n) => n * 5;
            
            Console.WriteLine("The value using a lambda expression is: {0}",
            delegObject2(5));

            Console.ReadLine();
        }
    }
}

Output:
The value using an anonymous method is: 25
The value using a lambda expression is: 25


A lambda expression can use two arguments, especially when you are
using the Standard Query Operators. Let us start by declaring the following
delegate that uses two arguments:

public delegate int MyDelegate(int m, int n);

You can instantiate the delegate by using a lambda expression like this:

MyDelegate myDelegate = (x, y) => x * y;

You can then invoke the delegate and display the result as follows:

Console.WriteLine("The product is: {0}", myDelegate(5, 4));
// output: 20

Different types of Object Initializers

Consider the bellow Point class

 class Point
    {
        int x, y;
        public int X
        {
            get { return x; }
            set { x = value; }
        }
        public int Y
        {
            get { return y; }
            set { y = value; }
        }
    }


Then consider the main Program

class Program
    {
        static void Main(string[] args)
        {
            Point c1=new Point();
            c1.X=10;
            c1.Y=100;
            Console.WriteLine(c1.X);
            Console.WriteLine(c1.Y);

            Point c2=new Point {X=20,Y=200};    // See the new type of Initializing

            Console.WriteLine(c2.X);
            Console.WriteLine(c2.Y);

            var p = new Point { X = 30, Y = 300 };    // See the new type of Initializing using "var" keyword

            Console.WriteLine(p.X);
            Console.WriteLine(p.Y);

            Console.ReadLine();
        }
    }

With complex fields, such as a square or a rectangle whose corners are
located at the points p1 and p2, you can create the Rectangle class as
follows:

    public class Rectangle
    {
        Point p1; Point p2;
        public Point ULcorner { get { return p1; } set { p1 = value; } }
        public Point LRcorner { get { return p2; } set { p2 = value; } }      
    }
You can create and initialize the Rectangle object like this:

         var rectangle = new Rectangle
        {
            ULcorner = new Point { X = 0, Y = 0 },
            LRcorner = new Point { X = 10, Y = 20 }
        };

Note that the semicolon at the end of the object initializer block.

Find current URL of an IE instance from a C# application

We all know that to get the current URL of the browser for an Web Application can be find by

HttpContext.Current.Request.Url.AbsolutePath

But to find out the current URL of IE from a C# application following scripts can be useful

            SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

            foreach (SHDocVw.WebBrowser ies in shellWindows)
            {
                Console.WriteLine(ies.LocationURL);
            }


Windows PowerShell 2.0 Best Practices (Best Practices (Microsoft))

Open an Internet Explorer from C# Application

First of all add reference the SHDocVw.dll to the apllication.

             object o = null;
            SHDocVw.InternetExplorer ie = new  SHDocVw.InternetExplorerClass();
            IWebBrowserApp wb = (IWebBrowserApp)ie;
            wb.Visible = true;

            //Do anything else with the window here that you wish
            wb.Navigate("http://google.com", ref o, ref o, ref o, ref o);

Internet Explorer 8, Illustrated Essentials

Serialisation and Deserialisation Class

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml.Serialization;

/// <summary>
/// Summary description for Serialization
/// </summary>
public class Serialization
{
    public static string StringSerialize(object instance)
    {
        MemoryStream m = new MemoryStream();
        XmlSerializer x = new XmlSerializer(instance.GetType());
        x.Serialize(m, instance);
        byte[] b = m.ToArray();
        string s = System.Text.ASCIIEncoding.ASCII.GetString(b);
        return s;
    }
    public static byte[] ByteSerialize(object instance)
    {
        MemoryStream m = new MemoryStream();
        XmlSerializer x = new XmlSerializer(instance.GetType());
        x.Serialize(m, instance);
        byte[] b = m.ToArray();
        return b;
    }
    public static object DeSerialize(string Text, Type type)
    {
        byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(Text);
        MemoryStream m = new MemoryStream(b);
        XmlSerializer x = new XmlSerializer(type);
        object o = x.Deserialize(m);
        return o;
    }
    public static object DeSerialize(byte[] b, Type type)
    {
        MemoryStream m = new MemoryStream(b);
        XmlSerializer x = new XmlSerializer(type);
        object o = x.Deserialize(m);
        return o;
    }
}