Geeks With Blogs
Server Error in Application An unhandled exception occurred during the execution of the current web request

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.

 

Related Post:

Association, Aggregation, Composition object relationship

Talent vs Attitude – Harsha Bhogle

What we can learn from Babies

 

Posted on Wednesday, September 27, 2006 10:46 AM ASP.NET , C# , Design Pattern , BizTalk , unhandled exception | Back to top


Comments on this post: Brief about Static modifier in C#

# re: Brief about Static modifier in C#
Requesting Gravatar...
can you discribe when we should use the static Constructor
Left by vivek on Sep 27, 2006 10:49 AM

# re: Brief about Static modifier in C#
Requesting Gravatar...
Static constructors are useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

Left by mahesh on Sep 27, 2006 10:50 AM

# re: Brief about Static modifier in C#
Requesting Gravatar...
Use static constructor to initialize the static members of the class.
Left by Mihir Solanki on Sep 27, 2006 12:56 PM

# Brief about Static modifier in C#
Requesting Gravatar...
Hi plzzz explain the scenario in which we will be using the static class
Left by Nishant Rana on Sep 23, 2007 7:36 PM

# re: Brief about Static modifier in C#
Requesting Gravatar...
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
Left by pratap das on Jan 30, 2008 6:15 PM

# re: Brief about Static modifier in C#
Requesting Gravatar...
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.
Left by pratap das on Jan 30, 2008 6:23 PM

Your comment:
 (will show your gravatar)


Copyright © Mahesh Singh | Powered by: GeeksWithBlogs.net