Why can’t the C# constructor infer type?

Is there a philosophical reason why the constructor can’t support type inference? No. When you have new Foo(bar) then we could identify all types called Foo in scope regardless of generic arity, and then do overload resolution on each using a modified method type inference algorithm. We’d then have to create a ‘betterness’ algorithm that … Read more

Is it unnecessary to put super() in constructor?

Firstly some terminology: No-args constructor: a constructor with no parameters; Accessible no-args constructor: a no-args constructor in the superclass visible to the subclass. That means it is either public or protected or, if both classes are in the same package, package access; and Default constructor: the public no-args constructor added by the compiler when there … Read more

Is it bad practice to have a constructor function return a Promise?

Yes, it is a bad practise. A constructor should return an instance of its class, nothing else. It would mess up the new operator and inheritance otherwise. Moreover, a constructor should only create and initialize a new instance. It should set up data structures and all instance-specific properties, but not execute any tasks. It should … Read more

Is it better to use Enumerable.Empty() as opposed to new List() to initialize an IEnumerable?

I think most postings missed the main point. Even if you use an empty array or empty list, those are objects and they are stored in memory. The Garbage Collector has to take care of them. If you are dealing with a high throughput application, it could be a noticeable impact. Enumerable.Empty does not create … Read more

How to inherit constructors?

Yes, you will have to implement the constructors that make sense for each derivation and then use the base keyword to direct that constructor to the appropriate base class or the this keyword to direct a constructor to another constructor in the same class. If the compiler made assumptions about inheriting constructors, we wouldn’t be … Read more

Does the default constructor initialize built-in types?

Implicitly defined (by the compiler) default constructor of a class does not initialize members of built-in types. However, you have to keep in mind that in some cases the initialization of a instance of the class can be performed by other means. Not by default constructor, nor by constructor at all. For example, there’s a … Read more

What’s the difference between an object initializer and a constructor?

Object Initializers were something added to C# 3, in order to simplify construction of objects when you’re using an object. Constructors run, given 0 or more parameters, and are used to create and initialize an object before the calling method gets the handle to the created object. For example: MyObject myObjectInstance = new MyObject(param1, param2); … Read more

Is it good practice to make the constructor throw an exception? [duplicate]

Throwing exceptions in a constructor is not bad practice. In fact, it is the only reasonable way for a constructor to indicate that there is a problem; e.g. that the parameters are invalid. I also think that throwing checked exceptions can be OK1, assuming that the checked exception is 1) declared, 2) specific to the … Read more

How does the const constructor actually work?

Const constructor creates a “canonicalized” instance. That is, all constant expressions begin canonicalized, and later these “canonicalized” symbols are used to recognize equivalence of these constants. Canonicalization: A process for converting data that has more than one possible representation into a “standard” canonical representation. This can be done to compare different representations for equivalence, to … Read more