Server Error in Application

An unhandled exception occurred during the execution of the current web request
posts - 60, comments - 76, trackbacks - 54

My Links

News

My Bookmarks

Archives

Post Categories

ASP.NET

Brief about Static modifier in C#

Static modifier used with the class, field, method, properties, operator, event and constructors. We can not use static modifier with indexers, destructors.

When we use static modifier with the members then they are no more part of instance of a class. Static members are a part of class itself.

 

For example:

public class MyClass

{

    public struct MyStruct

    {

        public static int x = 100;

    }

}

 

//To refer to the static member x, use the fully qualified name

MyClass.MyStruct.x

 

 

Consider a class that represents a student. Assume that the class contains a method to count student. Both the method does not belong to any instance student. Instead they belong to the College class. Therefore, they should be declared as static members of the college class.

 

Static Class

If static modifier is apply to the class then all member of class must be static.

Classes, including static classes, may have static constructors.

It is not possible to make an instance of static class with “new” keyword.

Features of static class:

1.      They can only contain static members.

2.      They are sealed means we cannot inherit the class.

3.      They can not  contain non static constructors

Static Members and Constructor:

Static members are initialized before the static member is accessed for the first time.

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

A static constructor does not take access modifiers or have parameters.

A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

 

Cheers!

Mahesh

maheshsingh21@hotmail.com


Print | posted on Wednesday, September 27, 2006 10:46 AM | Filed Under [ C# ]

Feedback

Gravatar

# re: Brief about Static modifier in C#

can you discribe when we should use the static Constructor
9/27/2006 10:49 AM | vivek
Gravatar

# re: Brief about Static modifier in C#

Static constructors are useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

9/27/2006 10:50 AM | mahesh
Gravatar

# re: Brief about Static modifier in C#

Use static constructor to initialize the static members of the class.
9/27/2006 12:56 PM | Mihir Solanki
Gravatar

#  Brief about Static modifier in C#

Hi plzzz explain the scenario in which we will be using the static class
9/23/2007 7:36 PM | Nishant Rana
Gravatar

# re: Brief about Static modifier in C#

when we declare a class as static

i) Object is not created in memory
ii) faster accesssing
iii) memory is utilized



If we declare any member as static in a class

i) we can access directly that member ofthat class from other class
i) we just need to have a static constructor for initializing that variable from class having static member
1/30/2008 6:15 PM | pratap das
Gravatar

# re: Brief about Static modifier in C#

Another Use of Static class


i) If we declare a static class it will not instantiate the class everytime it is called so that when we are Invoking a method from client side (like RMI in java) to remote server the same object will process request.
1/30/2008 6:23 PM | pratap das

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 8 and type the answer here:

Powered by: