Linked Questions
56 questions linked to/from Initialize class fields in constructor or at declaration?
12 votes
10 answers
9k views
Is it bad practice to initialise fields outside of an explicit constructor [duplicate]
Possible Duplicate: Initialize class fields in constructor or at declaration? We are arguing about coding practices. The examples here are a little too simple, but the real deal has several ...
13 votes
6 answers
29k views
Initialization of variables: Directly or in the constructor? [duplicate]
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? Most of the time, I see the way of initializing a variable like this public class Test { private ...
9 votes
9 answers
2k views
Poor practice to include code in default constructor [duplicate]
I worked with a developer that had many years my senior in C# experience. I no longer have a way to contact him and I remember him saying that it's not a good idea to include code in a default ...
5 votes
4 answers
10k views
Initializing Fields Value in Constructor Versus in Fields Declaration [duplicate]
As we know, in java and some other object-oriented programming languages, fields value can be set in constructor or be initialized in fields declaration statements. I want to know the essential ...
4 votes
5 answers
6k views
Difference between initialization at declaration and initialization in constructor [duplicate]
What is the difference between the following two, and which is more preferable?? public class foo { int i = 2; } public class foo { int i; foo() { i = 2; } }
4 votes
4 answers
7k views
Best practice for initializing member variables? [duplicate]
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? I am working with C# but this probably applies to Java (or any other language that allows this behavior ...
1 vote
1 answer
2k views
c# declaring variables inside or outside a constructor [duplicate]
I'm trying to understand the difference in variable declaration of a class inside or outside of a constructor. In this case creating the list for grades. To me, the below achieves the same result so ...
3 votes
6 answers
133 views
Declaring instances differences [duplicate]
What is the difference between the following code fragments? Form1 form1 = new Form1(); and Form1 form1; public Class1() { form1 = new Form1(); }
0 votes
4 answers
1k views
Differences between calling method inside or outside constructor [duplicate]
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? this is just a simple and even a little stupid doubt that me and my friends raised while talking about our ...
4 votes
2 answers
128 views
Difference in instancing object - C# basic [duplicate]
I am beginner in object oriented programming and I have one simple question. What is difference between: public class Calculation { private _externalObject = new ExternalClass(); public ...
1 vote
2 answers
1k views
difference between initializing on declaration and on object construction [duplicate]
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? Is there any difference between these two classes? public class Test { public Guid objectId = Guid....
1 vote
2 answers
155 views
c# Initializers vs constructors [duplicate]
I'm currently working through some old code and I started to wonder, is there actually any difference between: public class XmlExport : IXmlExport { private readonly IJobRepository jobRepository ...
3 votes
2 answers
164 views
using default constructor to initialize types C# [duplicate]
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? Please do you have any advantage for doing public class MyClass { List<string> list; public ...
0 votes
1 answer
161 views
When to initialize class variable in constructor and when not? [duplicate]
In what scenario would you initialise class variables in constructors and when without constructor? class CustomList { public Node head; public int count; public ...
0 votes
3 answers
112 views
Does it make any practical difference to instantiate an object inside the constructor? [duplicate]
Is there any practical difference between those two ways of instantiating an object? public class myClass { private myType myObject = new myType(); } and public class myClass { private myType ...