Skip to main content

All Questions

Tagged with
13 votes
4 answers
18k views

C# - Initialize DTOs in constructor or via properties?

Update: C# 9 should fix this problem using records :D I was wondering if there is a recommended approach to initializing the properties of a plain object that is used for data transfer, for example ...
AyCe's user avatar
  • 438
6 votes
2 answers
1k views

When to use Constructor and when to use Collection Initializer?

I am having a .Net class which has 10 properties like given below: [the datatypes of individual property is just a placeholder here and it can be anything from a primitive type to an object to a list ...
GawdePrasad's user avatar
3 votes
2 answers
306 views

For what reasons Java and C# initialize static data on demand?

I am reading "The Go Programming Language" right now and I have read package initialization chapter which tells (or I read it wrong) that Go uses eagerly initialization. So in time we saw say C++ ...
greenoldman's user avatar
  • 1,543
3 votes
1 answer
1k views

Why does this static field always get initialized over-eagerly?

I am looking at this excellent article from Jon Skeet. While executing the demo code, Jon Skeet says that we can expect three different kinds of behaviours. To quote that article: The runtime could ...
TheSilverBullet's user avatar
3 votes
8 answers
8k views

Conditionally initializing a string list

I am writing a program that needs to be able to process data from a number of different sources. The sources output data in a variety of formats. So, depending on which source is being used, I need to ...
TestEngineer's user avatar
2 votes
3 answers
4k views

How do you create immutable objects with many parameters?

I need to create an immutable object but it requires several parameters to work like: class FooRepo { public string ConnectionStringName { get; } public string SchemaName { get; } public ...
t3chb0t's user avatar
  • 2,602
2 votes
1 answer
9k views

Should I initialize a member variable in declaration when it is initialized by a constructor parameter?

Which is recommended for initialization of class fields in C#: class Foo { public X x = new X(); // or any default value... public Foo(X _x) { x = _x; } } Or class Foo { ...
Ahmad's user avatar
  • 1,866
2 votes
1 answer
118 views

Modelling seats of a table in a social game

Assume we want to model a table where players can sit down to get together to play a game (card games, dice games, ...). Assume a few properties associated with each seat class Seat { public int ...
Benj's user avatar
  • 169
1 vote
2 answers
696 views

Is it a good idea having default static variables for new instances?

OK let's say I have something like this: public class MyObject { public static int DefaultValue = 9 private int _value = DefaultValue; public int Value { get { return _value; } set { ...
user avatar
1 vote
3 answers
18k views

Make Return Type an Interface - Problem with Initialization

I would like to make the return type of my method an interface rather than a class for similar reasons stated in c# List or IList, however I am having trouble figuring out how to initialize the ...
Strategy Thinker's user avatar
0 votes
1 answer
254 views

Best way to accept a lot of user input from command line

I have a program that accepts computer names and then will perform CIM session tasks. The computer names are passed in from the user input and separated by "," (ex: program.exe -computers ...
James's user avatar
  • 11