Collections

Complete Collection Comparison

http://www.codeproject.com/Articles/832189/List-vs-IEnumerable-vs-IQueryable-vs-ICollection-v

https://www.youtube.com/watch?v=V-APRrWz_3o

What are the various methods of hosting a WCF service?

wcf hosting are three
self hosting
iis hosting
was window activation servies

  • Hosting in IIS6 only support HTTP protocols and "on-demand" activation
  • Hosting in IIS7 / WAS (only on Vista / Server 2008 and up) supports all protocols and "on-demand" activation
  • Self-Hosting in a console app or Windows service supports all protocols, but doesn't support on-demand activation (e.g. your service must be up and running all the time, it cannot be magically activated when a request comes in)

Three ways to do WCF instance management

WCF service object instancing basics

In normal WCF request and response communication, the following sequence of actions takes place:
  • WCF client makes a request to a WCF service object.
  • WCF service object is instantiated.
  • WCF service instance serves the request and sends the response to the WCF client.
Following is a pictorial representation of how WCF requests and responses work.

Following are different ways by which you can create WCF instances:
  • Create a new WCF service instance on every WCF client method call.
  • Only one WCF service instance should be created for every WCF client session.
  • Only one global WCF service instance should be created for all WCF clients.
To meet the above scenarios, WCF has provided three ways by which you can control WCF service instances:
  • Per call
  • Per session
  • Single instance

Per call instance mode

When we configure a WCF service as per call, new service instances are created for every method call you make via a WCF proxy client. The image below shows this in a pictorial format:
  • The WCF client makes the first method call (method call 1).
  • A new WCF service instance is created on the server for this method call.
  • The WCF service serves the request and sends the response and the WCF instance is destroyed and given to the garbage collector for clean up.
  • Now let’s say the WCF client makes a second method call, a new instance is created, the request is served, and the WCF instance is destroyed.
In other words, for every WCF client method call, a WCF service instance is created, and destroyed once the request is served.

How to implement WCF per call instancing

In order to specify the instancing mode, we need to provide the InstanceContextMode value in the ServiceBehavior attribute as shown below. This attribute needs to specified on the Service class. In the below code snippet, we have specified intCounter as a class level variable and the class counter is incremented by one when the Increment method is called.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Percall)] 
public class Service : IService
{
    private int intCounter;

    public int Increment()
    {
        intCounter++
        return intCounter;
    }
}
At the client, we consume the WCF client and we call the Increment method twice.
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient(); 
MessageBox.Show(obj.Increment().ToString());
MessageBox.Show(obj.Increment().ToString());
Even though we have called the Increment method twice, we get the value ‘1’. In other words, the WCF service instance is created for every method call made to the WCF service instance so the value will always be one.

Per session instance mode

Very often we need to maintain state between method calls or for a particular session. For those kinds of scenarios, we will need to configure the service per session. In per session, only one instance of a WCF service object is created for a session interaction. The figure below explains this in pictorial format.
  • The client creates the proxy of the WCF service and makes method calls.
  • A WCF service instance is created which serves the method response.
  • The client makes one more method call in the same session.
  • The same WCF service instance serves the method call.
  • When the client finishes its activity, the WCF instance is destroyed and served to the garbage collector for clean up.

How to implement per session instancing

To configure service as per session, we need to configure the ServiceBehavior attribute with a PerSession value in the InstanceContextMode object.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
public class Service : IService
{
    private int intCounter;
    public int Increment()
    {
        intCounter++
        return intCounter;
    }
}
At the client side, when we run the below client code, you should see the value ‘2’ after the final client code is executed. We have called the method twice so the value will be seen as two.
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient(); 
MessageBox.Show(obj.Increment().ToString());
MessageBox.Show(obj.Increment().ToString());

Single instance mode

Often we would like to create one global WCF instance for all WCF clients. To create a single instance of a WCF service, we need to configure the WCF service as Single instance mode. Below is a simple pictorial notation of how the single instance mode will operate:
  • WCF client 1 requests a method call on the WCF service.
  • A WCF service instance is created and the request is served. The WCF service instance is not destroyed, the service instance is persisted to server other requests.
  • Now let’s say some other WCF client, e.g., client 2, requests a method call.
  • The same WCF instance which was created by WCF client 1 is used to serve the request for WCF client 2. In other words, only one global WCF server service instance is created to serve all client requests.

How to implement single instance mode

In order to create a single instance of a WCF service, we need to specify InstanceContextMode as Single.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class Service : IService
{
}
If you call the WCF service from a different client, you will see the counter incrementing. The counter becomes a global variable.

When should you use per call, per session, and single mode?

Per call

  • You want a stateless services.
  • Your service holds intensive resources like connection objects and huge memory objects.
  • Scalability is a prime requirement. You would like to have a scaled out architecture.
  • Your WCF functions are called in a single threaded model.

Per session

  • You want to maintain states between WCF calls.
  • You a scaled up architecture.
  • Light resource references.

Single

  • You want share global data through your WCF service.
  • Scalability is not a concern.

References

 

Create a different alias instead of jQuery to use in the rest of the script.

var j = jQuery.noConflict();
// Do something with jQuery
j( "div p" ).hide();
// Do something with another library's $()
$( "content" ).style.display = "none";

Avoiding Conflicts with Other Libraries

The jQuery library and virtually all of its plugins are contained within the jQuery namespace. As a general rule, global objects are stored inside the jQuery namespace as well, so you shouldn't get a clash between jQuery and any other library (like prototype.js, MooTools, or YUI).
That said, there is one caveat: by default, jQuery uses $ as a shortcut for jQuery. Thus, if you are using another JavaScript library that uses the $ variable, you can run into conflicts with jQuery. In order to avoid these conflicts, you need to put jQuery in no-conflict mode immediately after it is loaded onto the page and before you attempt to use jQuery in your page.

Factory Design Pattern

Introduction

This article demonstrates how and when to use a factory design pattern. This article begins with the intent of the Factory Pattern. After that, it demonstrates a logical and physical model of factory. Then step-by-step implementation of factory pattern. In the end, the article discusses how it is useful.

Consider a Scenario

Consider a scenario where you developed your application for a SQL Server database. And in the future you need to shift from SQL Server to Oracle database. Then you have to change your code if you have not designed your application using a factory method.

Intent

Define an interface for creating an object, but let subclasses decide which class to instantiate.

Logical Model

LogicalModel.jpg

Rather than creating a product directly, the client uses a factory and the factory creates the product.

Physical ModelPhysicalModel.jpg

In physical model there is Factory which is an abstract class and implementation of this abstract class is in Concrete Factory. Similarly Product is an abstract class and implementation of this abstract class in Concrete Product.

Let's try to understand using a class diagram

FactoryPattern.jpg
  • Products : Declares an abstract class to define products
  • Concrete Product : It implements the product to define a concrete object
  • Factory : An abstract class, used to create a Factory
  • Concrete Factory : It implements the factory methods declared within the factory
Step 1
  1. Open Visual Studio
  2. File >> New >> Project >> Class Library
  3. Give it the name "Product"
  4. Add one class, "DALBaseClass"
  5. Copy the following segment of code:
     
using System;using System.Data;
 
namespace Product
{
    public abstract class DALBaseClass
    {
        private string strConnectionString;
        private IDbConnection connection;
        private IDbCommand command;
        private IDbTransaction transaction;

        public string ConnectionString
        {
           
get            {
              if (strConnectionString == string.Empty || strConnectionString.Length == 0)
                    throw new ArgumentException("Invalid database connection string.");
               
              return strConnectionString;
            }
           
set            { strConnectionString = value; }
        }

        protected DALBaseClass() { }

        public abstract IDbConnection GetDataProviderConnection();
       
        public abstract IDbCommand GeDataProviderCommand();
       
        public abstract IDbDataAdapter GetDataProviderDataAdapter();
 
        #region Database Transaction

        public string OpenConnection()
        {
            string Response = string.Empty;
           
try            {
             connection = GetDataProviderConnection();
// instantiate a connection object             //connection.ConnectionString = this.ConnectionString;             //connection.Open(); // open connection             Response = ((System.Reflection.MemberInfo)(connection.GetType())).Name + "
                        Open Successfully";
            }
            
catch            {
                connection.Close();
                Response = "Unable to Open " +   
                           ((System.Reflection.MemberInfo)(connection.GetType())).Name;
            }
            return Response;
        }
 
        #endregion    }
}

Step 2
  1. Right-click on the solution file
  2. Add >> New Project >> Class Library
  3. Give it the name "Concrete Products"
  4. Create the concrete product i.e. add classes for SQL, Oracle, ODBC and OleDb and inherit the DALBaseClass i.e. Product; see:
SQL Server

using System.Data;
using System.Data.SqlClient;
using Product;
namespace ConcreteProducts
{
    public class SqlDAL : DALBaseClass
    {
        // Provide class constructors
        public SqlDAL() { }

        public SqlDAL(string connectionString)
        {
            this.ConnectionString = "Data Source=xxxx;Initial Catalog=xxxx;
                                     User ID=xxxx;Password=xxxx";
        }

        // DALBaseClass Members
        public override IDbConnection GetDataProviderConnection()
        {
            return new SqlConnection();
        }

        public override IDbCommand GeDataProviderCommand()
        {
            return new SqlCommand();
        }

        public override IDbDataAdapter GetDataProviderDataAdapter()
        {
            return new SqlDataAdapter();
        }
    }
}

Oracle

using System.Data;
using System.Data.OracleClient;
using Product;

namespace ConcreteProducts
{
    public class OracleDAL : DALBaseClass
    {
        // Provide class constructors
        public OracleDAL() { }

        public OracleDAL(string connectionString)
        {
            this.ConnectionString = "Data Source=Oracle8i;Integrated Security=yes";
        }


        // DALBaseClass Members
        public override IDbConnection GetDataProviderConnection()
        {
            return new OracleConnection();
        }
        public override IDbCommand GeDataProviderCommand()
        {
            return new OracleCommand();
        }

        public override IDbDataAdapter GetDataProviderDataAdapter()
        {
            return new OracleDataAdapter();
        }
    }
}

ODBC

using System.Data;
using System.Data.Odbc;
using Product;

namespace ConcreteProducts
{
    public class OdbcDAL : DALBaseClass
    {
        // Provide class constructors
        public OdbcDAL() { }

        public OdbcDAL(string connectionString)
        {
            this.ConnectionString = "DSN=dsnname";
        }


        // DALBaseClass Members
        public override IDbConnection GetDataProviderConnection()
        {
            return new OdbcConnection();
        }

        public override IDbCommand GeDataProviderCommand()
        {
            return new OdbcCommand();
        }

        public override IDbDataAdapter GetDataProviderDataAdapter()
        {
            return new OdbcDataAdapter();
        }
    }
}

OleDb

using System.Data;
using System.Data.OleDb;
using Product;
namespace ConcreteProducts
{
    public class OleDbDAL : DALBaseClass
    {
         // Provide class constructors
        public OleDbDAL() { }

        public OleDbDAL(string connectionString)
        {
            this.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
                      Data Source=C:\\Development\\Mine\\DataAccessLayer\\Northwind.mdb";
        }


        // DALBaseClass Members
        public override IDbConnection GetDataProviderConnection()
        {
            return new OleDbConnection();
        }
        public override IDbCommand GeDataProviderCommand()
        {
            return new OleDbCommand();
        }

        public override IDbDataAdapter GetDataProviderDataAdapter()
        {
            return new OleDbDataAdapter();
        }
    }
}
In the above four classes you can observe one common thing which is implementation of a common abstract class.

In product there is three abstract methods and you can find the implementation of each of these three abstract methods in concrete classes.

For example in the class SqlDAL:

GetDataProviderConnection returns SqlConnection
GeDataProviderCommand returns SqlCommand
GetDataProviderDataAdapter returns SqlDataAdapter

In the same manner Oracle returns OracleConnection, OracleCommand and OracleDataAdapter. And the same for the ODBC and OleDb classes.

Now let's move on to the Factory.

Step 3
 

  1. Right-click on the solution file
  2. Add >> New Project >> Class Library
  3. Give name as Factory
  4. Create one class 'DALFactory'
using Product;
namespace Factory
{
    public enum DataProviderType
    {
        Access,
        Odbc,
        OleDb,
        Oracle,
        Sql
    }
    public abstract class DALFactory
    {
      public abstract DALBaseClass GetDataAccessLayer(DataProviderType dataProviderType);
    }
}
Here we create an abstract class DALFactory. We also create an enum to pass our db choice.

Step 4
 
  1. right-click on solution file
  2. Add >> New Project >> Class Library
  3. Give it the name "ConcreteFactory"
  4. Create one class "DbDALFactory"
using System;using ConcreteProducts;using Factory;using Product;namespace ConcreteFactory
{
    public class DbDALFactory : DALFactory
    {
       public override DALBaseClass GetDataAccessLayer(DataProviderType dataProviderType)
        {
            switch (dataProviderType)
            {
                case DataProviderType.Access:
                case DataProviderType.OleDb:
                    return new OleDbDAL();

                case DataProviderType.Odbc:
                    return new OdbcDAL();
                case DataProviderType.Oracle:
                    return new OracleDAL();
                case DataProviderType.Sql:
                    return new SqlDAL();

                default:
                    throw new ArgumentException("Invalid DAL provider type.");
            }
        }
    }
}
This class implements an abstract method of DALFactory i.e. Factory. And create an object of Concrete Product.

Now let's see how the client uses the factory.

Step 5

  1. Right-click on the solution file
  2. Add >> New Project >> Console Application
  3. Give it the name "Client"
using System;
using ConcreteFactory;
using Factory;

namespace FPClient
{
    class Program
    {
        static void Main(string[] args)
        {
            string choice = string.Empty, dbProvider = string.Empty;
            bool done = false;
            do
            {
                Console.Clear();
                Console.WriteLine("\t Select one of the Database Providers \n");
                Console.WriteLine("\t 1. Access / OleDb");
                Console.WriteLine("\t 2. Odbc");
                Console.WriteLine("\t 3. Oracle");
                Console.WriteLine("\t 4. Sql \n");
                Console.WriteLine("===============================================");
                Console.Write("\t Enter Your Selection (0 to exit) : ");
                choice = Console.ReadLine();
            
                switch (choice)
                {
                    case "0" :
                        done = true;
                        break;

                    case "1":
                        dbProvider = "Access";
                        break;

                    case "2":
                        dbProvider = "Odbc";
                        break;

                    case "3":
                        dbProvider = "Oracle";
                        break;

                    case "4":
                        dbProvider = "Sql";
                        break;
                }
                if (choice != "0")
                {
                    Console.WriteLine("===========================================");
                    DALFactory Factory = new DbDALFactory()
                    var DAL = Factory.GetDataAccessLayer((DataProviderType)Enum.Parse                                            (typeof(DataProviderType), dbProvider));
                    Console.WriteLine(DAL.OpenConnection());
                    Console.ReadKey();
                }
            } while (!done);
        }
    }
}

Here in the client code, we can see that the client creates an object of DALFactory i.e. Factory and Factory creates an object of Concrete Products. The main thing in this pattern is that the client is totally unaware of the concrete products.

Ref:http://msdn.microsoft.com/en-us/library/ee817667.aspx
http://www.codeproject.com/Articles/37547/Exploring-Factory-Pattern 
http://www.codeproject.com/Articles/716413/Factory-Method-Pattern-vs-Abstract-Factory-Pattern

Facade in C#

Façade defines a higher-level interface that makes the subsystem easier to use.


This structural code demonstrates the Facade pattern which provides a simplified and uniform interface to a large subsystem of classes.
using System;

  class MainApp
  {
    public static void Main()
    {
      Facade facade = new Facade();

      facade.MethodA();
      facade.MethodB();

      // Wait for user 
      Console.Read();
    }
  }

  // "Subsystem ClassA" 
  class SubSystemOne
  {
    public void MethodOne()
    {
      Console.WriteLine(" SubSystemOne Method");
    }
  }

  // Subsystem ClassB" 
  class SubSystemTwo
  {
    public void MethodTwo()
    {
      Console.WriteLine(" SubSystemTwo Method");
    }
  }

  // Subsystem ClassC" 
  class SubSystemThree
  {
    public void MethodThree()
    {
      Console.WriteLine(" SubSystemThree Method");
    }
  }

  // Subsystem ClassD" 
  class SubSystemFour
  {
    public void MethodFour()
    {
      Console.WriteLine(" SubSystemFour Method");
    }
  }

  // "Facade" 
  class Facade
  {
    SubSystemOne one;
    SubSystemTwo two;
    SubSystemThree three;
    SubSystemFour four;

    public Facade()
    {
      one = new SubSystemOne();
      two = new SubSystemTwo();
      three = new SubSystemThree();
      four = new SubSystemFour();
    }

    public void MethodA()
    {
      Console.WriteLine("\nMethodA() ---- ");
      one.MethodOne();
      two.MethodTwo();
      four.MethodFour();
    }

    public void MethodB()
    {
      Console.WriteLine("\nMethodB() ---- ");
      two.MethodTwo();
      three.MethodThree();
    }
  }
MethodA() ----
SubSystemOne Method
SubSystemTwo Method
SubSystemFour Method

MethodB() ----
SubSystemTwo Method
SubSystemThree Method

Dependency Injection Pattern

Dependency Injection : Instead of creating objects inside the class it allows you to inject objects
There are three types of injections are there 1) Construction Injection 2) Setter Injection 3) Interface Injection
Object Dependency Explained
When an object needs another object to operate properly, we say that the former is dependent on the latter. This behavior is transitive in nature. Consider three objects, namely, A, B, and C. If object A is coupled to object B, and B is in turn coupled to C, then object A is effectively coupled to object C—it is dependent on C. I've used the terms coupling and dependency interchangeably in this article.

Objects can be coupled in two ways: tight coupling and loose coupling. When an object is loosely coupled with another object, you can change the coupling with ease; when the coupling is tight, the objects are not independently reusable and hence are difficult to use effectively in unit test scenarios.

Here's an example of tight coupling. Consider two classes, C1 and C2, where C1 is tightly coupled with C2 and requires it to operate. In this case C1 is dependent on C2, as shown below:


   public class C2
   {
     //Some code
   }
   public class C1
   {
      C2 bObject = new C2();
      //Some code
   }
The tight coupling between the two classes shown above occurs because C1 (which is dependent on C2) creates and contains an instance of the class C2. It's "tight" because you can eliminate or change the dependency only by modifying the container class (C1). This is where dependency injection fits in.
What is Dependency Injection?
Dependency injection eliminates tight coupling between objects to make both the objects and applications that use them more flexible, reusable, and easier to test. It facilitates the creation of loosely coupled objects and their dependencies. The basic idea behind Dependency Injection is that you should isolate the implementation of an object from the construction of objects on which it depends. Dependency Injection is a form of the Inversion of Control Pattern where a factory object carries the responsibility for object creation and linking. The factory object ensures loose coupling between the objects and promotes seamless testability.

Advantages and Disadvantages of Dependency Injection
The primary advantages of dependency injection are:
  • Loose coupling
  • Centralized configuration
  • Easily testable
 =====================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private IDataAccess GetDataAccessFactory(string rqType)
        {
            string typeString = string.Empty;
            IDataAccess reqType = null;
           
            switch(rqType)
            {
                case  "Oracle":
                    typeString = "WindowsFormsApplication2.OracleDataAccess";
                    break;
                case "SQlServer":
                    typeString = "WindowsFormsApplication2.SqlDataAccess";
                    break;
            }

             Type xxx = Type.GetType(typeString);
            reqType = Activator.CreateInstance(xxx) as IDataAccess;

            return reqType;
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            //Type xxx = Type.GetType("WindowsFormsApplication2.SqlDataAccess");
           // Type xxx = Type.GetType("WindowsFormsApplication2.OracleDataAccess");

            //IDataAccess reqDataAccess = Activator.CreateInstance(xxx)  as IDataAccess;

            //DataAccess da = new DataAccess(Activator.CreateInstance(xxx) as IDataAccess);

            //MessageBox.Show(da.GetData());

            DataAccess da = new DataAccess(GetDataAccessFactory("Oracle"));

            MessageBox.Show(da.GetData());


            da.RequiredObject = GetDataAccessFactory("SQlServer");
            MessageBox.Show(da.GetData());

       
        }


        private DataTable GetFulfillmentOrderReleases(int fulfillmentOrderId)
        {
            SqlConnection objCon = new SqlConnection("User ID=Automation_Service;pwd=fran-fRe;Server=MNDEV02;Database=CDMS");
            SqlDataAdapter objAdp = new SqlDataAdapter("SELECT * FROM  FulfillmentOrderReleases WHERE iPK_OrderReleaseID = " + fulfillmentOrderId.ToString(),objCon);
            DataTable dt = new DataTable();
            objAdp.Fill(dt);

            return dt;
        }

        private List GetListForTable(DataTable dt) where T : new()
        {
            List reqList = new List();

            Type reqType = typeof(T);

            foreach (DataRow dr in dt.Rows)
            {

                //T reqObj = Activator.CreateInstance();
                T reqObj = new T();


                PropertyInfo[] propInfo = reqType.GetProperties();

                foreach (PropertyInfo prop in propInfo)
                {
                    prop.SetValue(reqObj, dr[prop.Name]);
                }
                reqList.Add(reqObj);
            }

            return reqList;
        }
    }

    public interface IDataAccess
    {
        string GetData();
    }


    public class DataAccess
    {
        private IDataAccess reqObject;
        public  IDataAccess RequiredObject
        {
            get
            {
                return reqObject;
            }
            set
            {
                reqObject = value;
            }

        }


        public DataAccess(IDataAccess pReqObject)
        {
            reqObject = pReqObject;
        }

        public string GetData()
        {
            return reqObject.GetData();
        }

    }

    public class SqlDataAccess : IDataAccess
    {
        public string GetData()
        {
            return "This data is comming from SQL";
        }
    }

    public class OracleDataAccess : IDataAccess
    {
        public string GetData()
        {
            return "This data is comming from Oracle";
        }
    }

    public class FulfillmentOrderReleases
    {
        public int iPK_OrderReleaseID  { get; set;}
        public int iFK_FulfillmentOrderID{get;set;}
        public int iFK_ParentOrderReleaseID { get; set; }
        public DateTime dt_DateEntered { get; set; }
        public int iFK_EnteredByID { get; set; }
        public int iFK_LocationID { get; set; }
        public DateTime dt_RequiredShipDate { get; set; }
        public int iFK_PreferredServiceLevelID { get; set; }
        public int i_Status { get; set; }
        public int b_Complete { get; set; }
        public DateTime dt_DateCompleted { get; set; }
        public string vch_PurchaseOrder { get; set; }
        public string vch_ERP_ID { get; set; }

    }


}
 

Event & EXTENSION METHODS


protected void Page_Load(object sender, EventArgs e) { Button objC = new Button(); } protected void Number_Handler(object sender, CommandEventArgs e) { switch (e.CommandName) { case "NAT": LoadNumbers(0, 1); break; case "EVE": LoadNumbers(0, 2); break; case "ODD": LoadNumbers(1, 2); break; } } private void LoadNumbers(int stratNumber, int stepNumber) { ListBox1.Items.Clear(); for (int i = stratNumber; i <= 100; i = i + stepNumber) { ListBox1.Items.Add(i.ToString()); } } ================================================ EVENTS ====== what is an event? An action recognized by an object. Event_handler contains the logic to handle the action. protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new Employee.SalaryDelegate(myhandler); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void myhandler(string EmpName, int Salary) { Response.Write(EmpName + " Salary " + Salary.ToString() + " is invalid"); } ========== public class Employee { public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event SalaryDelegate SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(EmpName, value); } } } } ================= namespace WebApplication17 { public class Employee { //public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event EventHandler SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(this, new EventArgs()); //SalaryEvent(null, null); } } } } } =================== protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new EventHandler(objEmp_SalaryEvent); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void objEmp_SalaryEvent(object sender, EventArgs e) { Employee tmpEmp = sender as Employee; Response.Write(tmpEmp.EmpName + " salary is invalid" ); } =================== public class Employee { //public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event EventHandler SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(null, new EmpEventData(this.EmpName, value)); } } } } public class EmpEventData : EventArgs { public string Name; public int salary; public EmpEventData(string pName, int psalary) { this.Name = pName; this.salary = psalary; } } =================== protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new EventHandler(objEmp_SalaryEvent); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void objEmp_SalaryEvent(object sender, EmpEventData e) { Response.Write(e.Name + " salar " + e.salary.ToString() + " is invalid"); } } =================== EXTENSION METHODS ================= 1) If no source code available 2) the class is sealed class but still you want to extend the functionalty of the clas then you can go for extentsion methods namespace StringExtender { public static class StringExtender { public static string ToProper(this string strValue) { StringBuilder sb = new StringBuilder(); string[] myWords = strValue.Split(' '); foreach (string st in myWords) { sb.Append(st.Substring(0, 1).ToUpper()); sb.Append(st.Substring(1).ToLower()); sb.Append(" "); } return sb.ToString(); } } } using StringExtender; namespace WebApplication17 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string st = "ravi kuMar KVS, gss Info TEch, Hyderabad"; Response.Write(st.ToProper()); } } }