0

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?

Faraz Ahmed
  • 1,467
  • 2
  • 18
  • 33
  • Since the confusion started with how WCF works by default, I believe that the other answer should be enough to let you figure out what's going on behind the scenes. – Matías Fidemraizer Mar 31 '16 at 06:10
  • 5
    the duplicate is obviously wrong! Yeah the OP mentioned WCF but really? – Random Dev Mar 31 '16 at 06:10
  • @Carsten I was going to answer it until I realized that the issue is with understanding stateless WCF services... – Matías Fidemraizer Mar 31 '16 at 06:11
  • The global variable is probably a static and shared among requests. See this question for more info: http://stackoverflow.com/questions/14154892/scope-of-static-variable-in-multi-user-asp-net-web-application – Krisztián Balla Mar 31 '16 at 06:11
  • I have over all confusion, not in WCF, so why this is make possible duplicate? If any other answer is exist which tells about constructor time then you have rights to make it duplicate.. @MatíasFidemraizer – Faraz Ahmed Mar 31 '16 at 06:11
  • 1
    @MatíasFidemraizer see the comments you get ... IMO the question was stated clear enough – Random Dev Mar 31 '16 at 06:12
  • @user6002727 About the part of constructors, I believe that you got confused about how many times are called because you don't understand stateless services at all, am I mistaken? – Matías Fidemraizer Mar 31 '16 at 06:13
  • Peace friends. There are several questions in the OP's post. Some may already be answered on the site. Some not. – Krisztián Balla Mar 31 '16 at 06:14
  • @user6002727 About the generic question of constructors... https://msdn.microsoft.com/en-us/library/ms173115.aspx – Matías Fidemraizer Mar 31 '16 at 06:14
  • @JennyO'Reilly I believe that this question could be closed just because it's too broad... But I prefered to mark it as duplicate because the source of the issue itself is understanding how WCF works by default – Matías Fidemraizer Mar 31 '16 at 06:16
  • I guess OP wants to know the class initialization order i.e in which order the variables, properties will be initialized and constructor will be called when the class instance is created. @user6002727 this might help you http://stackoverflow.com/questions/1882692/c-sharp-constructor-execution-order – Nitin Mar 31 '16 at 06:18
  • @user6002727 About the thing of "construction time", a constructor is just like a regular method. What does a regular method to take less or more time? More code lines? No, you could call a heavy operation in a single line, or you could even call a network operation with a lot of lag also in a single line... It's just obvious that the question has an indeterminate number of answers. It's too broad. – Matías Fidemraizer Mar 31 '16 at 06:19
  • **Question Updated** Kindly Review – Faraz Ahmed Mar 31 '16 at 06:20
  • @user6002727 I've checked your update. I highly suggest you that you rephrase your concerns in one or multiple questions. Be more concrete. And perform your own research before asking too. A simple console app with some breakpoints can be even more useful than a bunch of words explaining you how it works! :) – Matías Fidemraizer Mar 31 '16 at 06:22
  • sometimes No one understand real scenario and without asking they make decision this will impact other. Sorry for these words but my scenerio is different so why you have mark as duplicate? and I want to know constructor behaviour when it is initilize, so why this is related to WCF ? @MatíasFidemraizer – Faraz Ahmed Mar 31 '16 at 06:24
  • @user6002727 since SO is collaborative, the question can be reopened. From my point of view, if it's reopened, there's a high risk of getting it closed again as too broad or even because is a duplicate of other question – Matías Fidemraizer Mar 31 '16 at 06:26
  • If there is any Question Regarding this Topic, Please let me Know? I just want to clear my confusion, nothing else. @MatíasFidemraizer – Faraz Ahmed Mar 31 '16 at 06:28
  • From how I gather, a viable answer for this question is to explain what happens when during class initialization and when what code is executed. – atlaste Mar 31 '16 at 06:35
  • Apparently, *How long do constructors a take to execute* == *Are WCF services stateless*. **ಠ_ಠ** – Gediminas Masaitis Mar 31 '16 at 06:44
  • I already said that the question as it was composed when the original poster published it, it pointed to a confusion on how wcf instantiates services, and this has driven the original poster to the other questions... – Matías Fidemraizer Mar 31 '16 at 06:48
  • @user6002727 Why don't you re-phrase the entire question instead of apending new content? If your concern is about what happens with class members when they're called from the constructor, just ask for it, there's no problem. My concern was you talked about the issue with WCF, and I still think that the other questions are too basic and you should contextualize them with some concrete issue, but again, try to re-phrase your question in one, two paragraphs in a more generic way and I'll re-open it... – Matías Fidemraizer Mar 31 '16 at 07:24
  • @user6002727 Also, edit your title... just read it and then check your question's body... it seems like you're just asking for construction time... and later you ask other questions inside :\ – Matías Fidemraizer Mar 31 '16 at 07:32
  • @MatíasFidemraizer yup, also my fault, I apologize and edit my Title. – Faraz Ahmed Mar 31 '16 at 08:53

1 Answers1

0

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();.

JC Borlagdan
  • 3,318
  • 5
  • 28
  • 51