This question already has answers here:
Setting properties via object initialization or not : Any difference ?
(5 answers)
Is there any benefit of using an Object Initializer?
(5 answers)
Closed 3 years ago.
This morning, I had a discussion with a co-worker regarding the following two example of initializing the code.
Example 1:
var employee = new Employee();
employee.FirstName = GetFirstName(); // this may get values from db
Example 2:
var employee = new Employee()
{
FirstName = GetFirstName()
}
My co-worker's argument was that example 2 is more efficient and if that object has any errors during initialization, it is automatically removed from heap. I am thinking that the compiled version of the code would look similar to example 1.
My question is that is my co-worker correct in his argument? When should you use property assignment instead of object initialization? Are there any best practices around this? MS doesn't have anything ...
Related
This question already has answers here:
Is it a good/acceptable practice to declare variable as interface type?
(5 answers)
Using Interface variables
(12 answers)
Closed 4 years ago.
I see this being done so often with ICollection and IEnumberable, seeing new objects being created from the Interface instead of the class itself. To make this simple, let's use IList vs List because I understand those two a lot more.
What is the difference between:
IList<string> People = new List<string>();
vs
List<string> People = new List<string>();
?
Similarly to IEnumerable and ICollections, can't you just use
Collections<string> People = new Collections<string>();
as well instead of "ICollections"? It's so confusing and why is this done?
This question already has answers here:
Why is there a default instance of every form in VB.Net but not in C#?
(2 answers)
Objects implicitly instantiated in vb.net?
(2 answers)
Closed 7 years ago.
Recently I've picked an old project in VB.NET and I keep finding things like this that annoy me:
Dim answer as String
answer = Form_Input_Text.Lbl_Answer.Text
Apparently, in VB.NET it is allowed to access stuff which should belong to an instance of a Form as if it is something static. In C# one would need to write this:
string answer = null;
using (Form_Input_Text my_form = new Form_Input_Text())
{
//stuff
answer = my_form.Lbl_Answer.Text;
}
The first bit of code is compiled with no errors, but sometimes I get NullReferenceException as expected.
How do I enforce this behaviour on VB.NET?
This question already has answers here:
Programmatic equivalent of default(Type)
(14 answers)
Closed 8 years ago.
So, I need to retrieve all properties of an instance which currently have a value that matches the default value of their respective type. Something along the lines of
GetType().GetProperties().Where(x =>
x.GetValue(this).Equals(default(x.PropertyType)));
This obviously doesn't work because it seems 'x' cannot be resolved anymore at this point. What could I do?
The problem is slightly different. You cannot pass a runtime instance of Type to default. Your problem can be simplified to this:
var type = typeof (string);
var defaultValue = default(type); // doesn't work
That doesn't work. Instead, you want to get the default value at run time, which has been answered by this question.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# variable initializations vs assignment
Just like in the title, could someone please explain what is the difference between Initialization and Assignment in C#? I'm preparing for a test and I wanted to know what's the best way to answer this type of question. Thanks
Cheers,
n1te
When you initialize a variable you're declaring it into existence.
PlasticCup mySippyCup = new PlasticCup();
When you assign, you're just saying "this water" goes into "this cup". The cup already exists.
mySippyCup = new PlasticCup();
Initialization is assigning value while declaring the variable - int a = 1
Assignment is just assigning value to a variable - a = 1
By this definition, some say all initializations are assignments, but all assignments are not initializations.
This question already has answers here:
Implicit typing; why just local variables?
(6 answers)
Closed 8 years ago.
Why is it not possible to have implicitly-typed variables at a class level within C# for when these variables are immediately assigned?
ie:
public class TheClass
{
private var aList = new List<string>();
}
Is it just something that hasn't been implemented or is there a conceptual/technical reason for why it hasn't been done?
Here's a blog post from Eric that explains the reasoning.