Tag: 构造函数

Stack 构造函数在从其他堆栈初始化时是否反转堆栈?

这是代码: var s = new Stack(); s.Push(1); s.Push(2); s.Push(3); s.Push(4); var ns = new Stack(s); var nss = new Stack(new Stack(s)); 然后让我们看看结果 tbLog.Text += “s stack:”; while(s.Count > 0) { tbLog.Text += s.Pop() + “,”; } tbLog.Text += Environment.NewLine; tbLog.Text += “ns stack:”; while (ns.Count > 0) { tbLog.Text += ns.Pop() + “,”; } tbLog.Text […]

从具有参数的“Form”inheritance

我有一个名为ScanFolder的表单,我需要另一个表单,它需要与ScanFolder非常相似,所以我决定使用表单inheritance 。 但是构造函数似乎存在一些误解。 ScanFolder看起来像: public partial class ScanFolder : Form { public ScanFolder(MainForm parent, bool[] autoModes, GlobalMethods GMethodsClass) { //Doing something with parameters } } 我试图像这样inheritanceForm : public partial class Arch2 : ScanFolder { } 但我收到警告未找到类型’mhmm.ScanFolder’的构造函数,并且Arch2表单编辑模式也出现错误,我看到调用堆栈错误。 所以我尝试过这样的事情: public partial class Arch2 : ScanFolder { public Arch2(MainForm parent, bool[] autoModes, GlobalMethods GMethodsClass) : base(parent, autoModes, GMethodsClass) […]

使用隐式/显式转换而不是构造函数的原因是什么?

一个例子是: XNamespace ns = “my namespace” 为什么不?: XNamespace ns = new XNamespace ( “my namespace” ) 使用隐式/显式转换而不是构造函数背后的想法是什么? 方便? 有这方面的指导方针吗?

私有构造函数和公共参数构造函数

我听说私有构造函数阻止了外部世界的对象创建。 当我有一个代码 public class Product { public string Name { get;set;} public double Price {get;set;} Product() { } public Product(string _name,double _price) { } } 在这里我仍然可以声明一个公共构造函数(参数),它不会破坏私有构造函数的目的吗? 我们什么时候需要代码中的私有和公共构造函数(参数)? 我需要详细说明。

如何告诉inheritance类不调用其基类’无参数构造函数?

我很惊讶地发现,无论何时调用派生类中的任何构造函数,都会调用我的基类的无参数构造函数。 我认为这就是: base()用于显式调用基础构造函数,如果我想要的话。 在实例化派生类时,如何防止调用基本构造函数? using System; namespace TestConstru22323 { class Program { static void Main(string[] args) { Customer customer = new Customer(“Jim”, “Smith”); Customer c = new Customer(); Console.WriteLine(customer.Display()); Console.ReadLine(); } } public class Person { public Person() { Console.WriteLine(“I don’t always want this constructor to be called. I want to call it explicitly.”); } […]

有没有办法在所有构造函数运行后立即自动调用特定方法?

我希望能够在构造派生对象时自动调用特定方法,但是我想不出怎么做。 以下代码说明。 另一个答案推荐OnLoad,但我在Mac上为Unity做这个,OnLoad似乎不支持我的平台。 有什么建议? public class Parent { public Parent () { // A. Stuff to do before child constructor code runs DoThisAutomaticallyAfterConstruction(); } public void DoThisAutomaticallyAfterConstruction() { // C. In this example, this will run after A, before B. I want it to run ABC } } public class Child : Parent { public […]

执行代码后调用构造函数库

假设我们有A类和B类.BlassB扩展了A类。(ClassB:ClassA) 现在让我们说每当我实例化ClassB时,我想运行一些随机代码,然后调用“base”来达到ClassA构造函数。 喜欢: class ClassA { public ClassA() { Console.WriteLine(“Initialization”); } } class ClassB : ClassA { public ClassB() //: base() { // Using :base() as commented above, I would execute ClassA ctor before // Console.WriteLine as it is below this line… Console.WriteLine(“Before new”); //base() //Calls ClassA constructor using inheritance //Run some more Codes here… […]

为什么基类必须有一个带0参数的构造函数?

这不会编译: namespace Constructor0Args { class Base { public Base(int x) { } } class Derived : Base { } class Program { static void Main(string[] args) { } } } 相反,我收到以下错误: ‘Constructor0Args.Base’不包含带0参数的构造函数 这是为什么? 基类是否需要一个带0参数的构造函数?

使用Moq来模拟构造函数?

我有这样一组构造函数: public BusinessObjectContext() : this(CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”).TableEndpoint.ToString(), CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”).Credentials) {} public BusinessObjectContext(string dataConnectionString) : this(CloudStorageAccount.Parse(dataConnectionString).TableEndpoint.ToString(), CloudStorageAccount.Parse(dataConnectionString).Credentials) { } public BusinessObjectContext(String baseAddress, StorageCredentials credentials) : base(baseAddress, credentials) { } 但是在测试/模拟时我需要没有任何连接字符串参数的对象。 我怎么能这样做 – 最好是在Moq? 这有可能吗?

为什么新的String(“Hello”)在C#中无效?

制作背后的逻辑/原因是什么? String s= new String(“Hello World”); C#中是非法的? 错误是 `string.String(char *)’的最佳重载方法匹配有一些无效的参数 我对API文档不感兴趣,我感兴趣的是为什么这是非法的。 是因为汇集静态字符串? 像Java池整数(-128)到整数(127)与可怕的结果? (当然还有字符串)