constructor

What is the return type of a constructor in C#?

一笑奈何 提交于 2019-12-17 10:35:24
问题 I have asked this question for Java on this link I got some answers in java.Now i want to know it in C#. As we know the we do not have to add any return type to a C# constructor. class Sample{ ..... Sample(){ ........ } } In Objective C, if we create a constructor, it returns a pointer to its class. But it is not compulsory, I think. AClass *anObject = [[AClass alloc] init];//init is the constructor with return type a pointer to AClass Similarly, Is the constructor converted to a method which

a constructor as a delegate - is it possible in C#?

喜夏-厌秋 提交于 2019-12-17 10:33:24
问题 I have a class like below: class Foo { public Foo(int x) { ... } } and I need to pass to a certain method a delegate like this: delegate Foo FooGenerator(int x); Is it possible to pass the constructor directly as a FooGenerator value, without having to type: delegate(int x) { return new Foo(x); } ? EDIT: For my personal use, the question refers to .NET 2.0, but hints/responses for 3.0+ are welcome as well. 回答1: Nope, the CLR does not allow binding delegates to ConstructorInfo . You can

How can I access a private constructor of a class?

我怕爱的太早我们不能终老 提交于 2019-12-17 10:27:26
问题 I am a Java developer. In an interview I was asked a question about private constructors: Can you access a private constructor of a class and instantiate it? I answered 'No' but was wrong. Can you explain why I was wrong and give an example of instantiating an object with a private constructor? 回答1: One way to bypass the restriction is to use reflections: import java.lang.reflect.Constructor; public class Example { public static void main(final String[] args) throws Exception { Constructor

Static factory methods vs Instance (normal) constructors?

懵懂的女人 提交于 2019-12-17 10:24:49
问题 In a language where both are available, would you prefer to see an instance constructor or a static method that returns an instance? For example, if you're creating a String from a char[] : String.FromCharacters(chars); new String(chars); 回答1: In Effective Java, 2nd edition, Joshua Bloch certainly recommends the former. There are a few reasons I can remember, and doubtless some I can't: You can give the method a meaningful name. If you've got two ways of constructing an instance both of which

Closing a form during a constructor

一世执手 提交于 2019-12-17 09:37:35
问题 Is it possible to close a form while the constructor is executing (or simply to stop it showing at this stage)? I have the following code: public partial class MyForm : Form { public MyForm() { if (MyFunc()) { this.Close(); } } } Which throws an ObjectDisposedException in Main(), here: static void Main() { ... // Following line errors Application.Run(new MyForm()); } I’ve tried checking the result of MyForm like this: static void Main() { ... MyForm frm = new MyForm(); if (frm != null) { //

C# inheritance and default constructors

空扰寡人 提交于 2019-12-17 09:37:13
问题 Suppose there is a base class A and a class B derived from A . Then, we know that the constructor of class A is never inherited by class B . However, when a new object of B is created, then - the default constructor of the class A is called prior to the default/custom constructor of class B is invoked. Maybe the purpose of this is that the fields of class A need to be initialized to default values. Now, suppose that class A has defined a custom constructor. This means that the default

Forwarding all constructors in C++0x

廉价感情. 提交于 2019-12-17 09:30:26
问题 What is the correct way to forward all of the parent's constructors in C++0x? I have been doing this: class X: public Super { template<typename... Args> X(Args&&... args): Super(args...) {} }; 回答1: There is a better way in C++0x for this class X: public Super { using Super::Super; }; If you declare a perfect-forwarding template, your type will behave badly in overload resolution. Imagine your base class is convertible from int and there exist two functions to print out classes class Base {

Java: accessing private constructor with type parameters

女生的网名这么多〃 提交于 2019-12-17 08:47:49
问题 This is a followup to this question about java private constructors. Suppose I have the following class: class Foo<T> { private T arg; private Foo(T t) { // private! this.arg = t; } @Override public String toString() { return "My argument is: " + arg; } } How would I construct a new Foo("hello") using reflection? ANSWER Based on jtahlborn's answer, the following works: public class Example { public static void main(final String[] args) throws Exception { Constructor<Foo> constructor;

Select class constructor using enable_if

十年热恋 提交于 2019-12-17 08:32:31
问题 Consider following code: #include <iostream> #include <type_traits> template <typename T> struct A { int val = 0; template <class = typename std::enable_if<T::value>::type> A(int n) : val(n) {}; A(...) { } /* ... */ }; struct YES { constexpr static bool value = true; }; struct NO { constexpr static bool value = false; }; int main() { A<YES> y(10); A<NO> n; std::cout << "YES: " << y.val << std::endl << "NO: " << n.val << std::endl; } I want to selectively define constructor A::A(int) only for

Delphi: Understanding constructors

喜你入骨 提交于 2019-12-17 08:25:25
问题 i'm looking to understand virtual override overload reintroduce when applied to object constructors. Every time i randomly add keywords until the compiler shuts up - and (after 12 years of developing with Delphi) i'd rather know what i'm doing, rather than trying things randomly. Given a hypothetical set of objects: TComputer = class(TObject) public constructor Create(Cup: Integer); virtual; end; TCellPhone = class(TComputer) public constructor Create(Cup: Integer; Teapot: string); virtual;