Significance of static method - C#.NET

Explain the significance of static method

Static methods are used when we want only one copy of that method to perform action and remain active at a single point in time.

Imagine a scenario where you need a class to connect to the database once and remain as it is for the entire application.
public static class MyDbConnection
{
   public static void ConnectToDb()
   {
        //code to connect to database
   }
}
public void SomeMethod()
{
    MyDbConnection.ConnectToDb();
}
You don’t want different instances of this class to be created and connect to the database again and again, hence make a static method in a static class.
Uses of boxing and unboxing - C#.NET
Explain the uses of boxing and unboxing - Boxing and Unboxing are used to convert value types into reference types and vice versa...
How to use the finally statement - C#.NET
Explain how to use the finally statement - Finally statement is executed always irrespective of any condition or error occurring in a method...
Steps to create a .NET Assembly - C#.NET
Explain the steps to create a .NET Assembly - Add a project from templates called “Class Library”...
Post your comment
Discussion Board
Statis methods
I am working on a website in ASP.net with c#. I am not expert in asp.net but working since 2 years with basic knowledge.
I have used static class and in that I work on basic things like session objects adding, mail sending, XML file reading etc. Even a Sqlconnection is defined static & I use it everywhere in my website. ex. Class name is general, & I use general.con to access database.
I did it coz I thought it will enhance my site performance. I want to ask m I right.
I dont know much ajax, so I wanted to enhance performance with my server side code. Please help me.
Rahul 01-12-2012