A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded. Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. the use of static class is... there is not any dependancy to each other methods or varibleas, then can leave entirly alone... the best way to use static class is to put all the common function or code at the same place, which does not need to intract with each other, also they dont use much memory space as they always in single mode.
Example ...
public static class Common { public static string GetNameFromID(int ID) { string returnStr = ""; //do something..... set value in returnStr variable.... return returnStr; } }
public class UserClass { public string GetDataFromStaticClass(int ID) { // no need to create object of common class to access the method.. return Common.GetNameFromID(ID) } }
Define the static member and function which are using in whole application by class name because static class initiates by CLR so we can't define its object so we call all static member in class by class name.
It is use to assign values to all static variable by constructor because we know that we can't create an object of static class so its calls only once when any one static variable /method will call. suppose you have two static member(property/method) then the constrcutor will call only once when we first call any static member but when we call second member then constructor will not call.
Static member are store in application domain so these gives same value in whole application until we restart our server means iisreset.
It is inilitize all variable at once.
we know that static constructor call only once by CLR when any static member call so we can't pass parameter in static constructor because CLR does not know about static class constructor parameter.
For Example Create a static Class:
namespace ConsoleApplication { public static class Command { static string mDeleteCommand = string.Empty; static string mInsertCommand = string.Empty; static Command() { mDeleteCommand = "Delete Command"; mInsertCommand = "Insert Command"; } public static string DeleteCommand { set { mDeleteCommand = value; } get { return mDeleteCommand; }
} public static string InsertCommand { set { mInsertCommand = value; } get { return mInsertCommand; }
} } }
and Access this class using System;
namespace ConsoleApplication { class Program { static void Main(string[] args) { string insertCommand = Command.InsertCommand; Command.DeleteCommand = "Delete";
There will be constructor of static type. All member will be static type. I will suggest you debug the code and see behave that constructor call once while properties calls two times.
Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.
Jignesh TrivediPosted Nov 2, 2012, 12:05 AM
A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods.
the use of static class is... there is not any dependancy to each other methods or varibleas, then can leave entirly alone... the best way to use static class is to put all the common function or code at the same place, which does not need to intract with each other, also they dont use much memory space as they always in single mode.
Example ...
public static class Common
{
public static string GetNameFromID(int ID)
{
string returnStr = "";
//do something..... set value in returnStr variable....
return returnStr;
}
}
public class UserClass
{
public string GetDataFromStaticClass(int ID)
{
// no need to create object of common class to access the method..
return Common.GetNameFromID(ID)
}
}
hope this will help you.
Sandeep Singh ShekhawatPosted Nov 1, 2012, 1:26 AM
Define the static member and function which are using in whole application by class name because static class initiates by CLR so we can't define its object so we call all static member in class by class name.
It is use to assign values to all static variable by constructor because we know that we can't create an object of static class so its calls only once when any one static variable /method will call. suppose you have two static member(property/method) then the constrcutor will call only once when we first call any static member but when we call second member then constructor will not call.
Static member are store in application domain so these gives same value in whole application until we restart our server means iisreset.
It is inilitize all variable at once.
we know that static constructor call only once by CLR when any static member call so we can't pass parameter in static constructor because CLR does not know about static class constructor parameter.
For Example Create a static Class:
namespace ConsoleApplication
{
public static class Command
{
static string mDeleteCommand = string.Empty;
static string mInsertCommand = string.Empty;
static Command()
{
mDeleteCommand = "Delete Command";
mInsertCommand = "Insert Command";
}
public static string DeleteCommand
{
set { mDeleteCommand = value; }
get { return mDeleteCommand; }
}
public static string InsertCommand
{
set { mInsertCommand = value; }
get { return mInsertCommand; }
}
}
}
and Access this class
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string insertCommand = Command.InsertCommand;
Command.DeleteCommand = "Delete";
Console.WriteLine(insertCommand + " " + Command.DeleteCommand);
Console.ReadLine();
}
}
}
There will be constructor of static type.
All member will be static type.
I will suggest you debug the code and see behave that constructor call once while properties calls two times.
Hope it will be helpful for you.
Thanks
Sandeep
Rohatash KumarPosted Nov 1, 2012, 1:21 AM
static class?
Satyapriya NayakPosted Nov 1, 2012, 12:52 AM
http://msdn.microsoft.com/en-us/library/79b3xss3%28v=vs.80%29.aspx
http://www.codeproject.com/Articles/11857/Introducing-C-2-0-static-classes