All Questions
Tagged with constructors c#
32 questions
39
votes
9
answers
50k
views
Constructor parameter validation in C# - Best practices
What is the best practice for constructor parameter validation?
Suppose a simple bit of C#:
public class MyClass
{
public MyClass(string text)
{
if (String.IsNullOrEmpty(text))
...
33
votes
3
answers
19k
views
Optional parameters or overloaded constructors
I am implementing a DelegateCommand, and when I was about to implement the constructor(s), I came up with the following two design choices:
1: Having multiple overloaded constructors
public ...
23
votes
3
answers
16k
views
how complex a constructor should be
I am having a discussion with my co-worker on how much work a constructor can do. I have a class, B that internally requires another object A. Object A is one of a few members that class B needs to do ...
22
votes
4
answers
5k
views
Constructing an object: should I expose or hide parameters passed to the constructor?
I've a habit I just mechanically do without even thinking too much about it.
Whenever a constructor is waiting for some parameters, I consider this a public information that should be available by ...
12
votes
2
answers
8k
views
Unit Test to test the creation of a Domain Object
I have a Unit Test, which looks like this:
[Test]
public void Should_create_person()
{
Assert.DoesNotThrow(() => new Person(Guid.NewGuid(), new DateTime(1972, 01, 01));
}
I am asserting that ...
11
votes
3
answers
16k
views
Stubbing Properties with private setters for tests
We have the object
public class MyObject{
protected MyObject(){}
public string Property1 {get;private set;}
public string Property2 {get;private set;}
public string Property3 {get;...
10
votes
2
answers
847
views
Is it bad to create classes whose sole purpose is to be converted to another class implicitly?
Imagine a situation where we're using a library that allows you to create Circle objects, where you can specify the radius and the center of the circle to define it. However, for some reason, it also ...
8
votes
4
answers
706
views
Checking the result of a constructor in C#
I'm working on a code base with a coworker who has a habit of checking the results of a constructor for a null in a fashion similar to this
Person p = new Person();
if (p != null)
{
p.Name = "...
7
votes
4
answers
2k
views
Can I make my constructors less repetitive?
I'm extending a class with 10 different constructors. The new subclass, SpecialImage, is used like this:
SpecialImage specialImage = new SpecialImage(..);
// Leverage the Rotate() method of ...
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 ...
4
votes
4
answers
9k
views
Why can't I call a constructor in itself?
I am currently porting the class NumberRange from Java to C#.
I am writing this constructor and I wonder whether I can call a constructor in itself. Something like this:
public NumberRange(Double ...
3
votes
4
answers
875
views
Giving a class many constructors and assigning via them as many properties as possible
I have written a class which represents a SQLite Trigger.
public SQLiteTrigger(string Name,
string On,
TriggerStartType StartType,
...
3
votes
4
answers
5k
views
Public static method calls private constructor
I'm working in a C# codebase that was largely written by a former developer and this pattern is used extensively...
public class AuditInserter
{
public static void Insert(
DataContext ...
3
votes
4
answers
348
views
Primary constructors for depedency injection
Microsoft identifies dependency injection as one of the uses for primary constructors.
public class ExampleController(IService service) : ControllerBase
{
[HttpGet]
public ActionResult<...
3
votes
5
answers
16k
views
Constructor overloading or allow null?
Which is the preferred design to use, one constructor that allows null, or two constructors where one throws an ArgumentNullException on null?
Two constructors with exception throwing
public class ...