C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

When static constructor is invoked?


Posted Date: 28 Sep 2008      Posted By: partha chowdhury      Member Level: Silver     Points: 1   Responses: 4



When static constructor is invoked?




Responses

Author: Selva    28 Sep 2008Member Level: SilverRating: 3 out of 53 out of 53 out of 5     Points: 3

Hi,
Static constructors are used for initializing the static data members. Also it is useful when the action needs only once.

Static constructor is invoked before the first instance of the class is created. Otherwise any static member is referred.

class A {
static A() { //Static constructor
//
}
}



Author: sairam kolasani    29 Sep 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Static Constructor invokes when ur creating first instance of the class.

These are used to initialise static members of the class.



Author: Vivek    29 Sep 2008Member Level: GoldRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

There are two types of constructors: instance constructors and static constructors. Instance constructors are constructors that initialize objects. Static constructors are constructors that initialize classes.

Initializing Classes at Load Time
C# ensures that a class is always initialized before it is used in code in any way.
This guarantee is achieved by using static constructors.
You can declare a static constructor like an instance constructor but prefix it
with the keyword static, as follows:
class Example
{
static Example( ) { ... }
}

The static constructor for a class is executed before any instances of the class are created.
The static constructor for a class is executed before any static member of the class is referenced.
The static constructor for a class is executed before the static constructor of any of its derived classes is executed.
The static constructor for a class never executes more than once.



Author: Miss Meetu Choudhary    13 Feb 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 5

A procedure or function or method that is used for creating and initializing objects. In any language, constructor is called after the object has been created and is used mainly to initialize the object's internal state i.e. there member variables. It is a special method that initializes an object when the object is created. In the class, a constructor has the same name as the class.


we have following types of constructors

1. copy constructor
2. default constructor
3. naval constructor
4. universal constructor
5. virtual constructor
6. constructor function
7. constructor brabham
8. taller del constructor

e.g. of copy constructor

class a
{
public int x;
public a()
{
x=10;
}
public a(class z) \\ copy constructor
{
x=100;
}
}
class b
{
public static void Main()
{
a obj1 = new a();
System.Console.WriteLine(x) ;
b obj2 = new a(obj1);
System.Console.WriteLine(x) ;

}
}
output
10
100;

Thanks and Regards
Miss Meetu Choudhary (Site Coordinator)
Go Green Save Green
My Profile on Google



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : What is the use of base,new,virtual and override keywords?
Previous : When compiler adds a default constructor to a class?
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use