简体   繁体   中英

What happen when new Instance of an object call?

There is one confusion in my mind, about the constructor of class. How ever I tried to find this but doesn't find any answer relevant to my confusion..

Suppose I have a class

public class mySampleClass
{
    public mySampleClass()
    {
        // This is the constructor method.
    }
    // rest of the class members goes here.
}

which have many properties, when I initialize this class what happen? I mean to say whether only constructor is being called? or something else?

what about rest of the properties? i am asking this foolish question because of my WCF service, which contain many methods, in every methods I have initialize same class, if I make object globally it crashes some where.

My other question is how many time it takes to initialize new instance of constructor? depending of all code? or whether constructor body?

Please elaborate with some Example. with two constructor or many.

UPDATE :

There is some confusion Regarding this Question, I have simply share one Scenario like WCF services, but I have to known all over constructor initialization Time, whether it depends on constructor? or all over object (containing other methods properties).

In simple words

I want to know constructor behaviour when it is initilize whether it is depend on Properties, Method etc?

Constructor gets executed only once and it's when you instantiate your class in other classes or methods or even in your service.

ex.

mySampleClass msc = new mySampleClass();

Properties and Methods inside the class won't be affected "unless" you do something like msc.PropertyName="somethingelse"; or msc.MethodName(); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM