需要澄清c#中的构造函数 [英] need clarification for Constructors in c#

查看:64
本文介绍了需要澄清c#中的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我对构造函数的理解是否正确。我只是试图尝试对构造函数进行测验,但对我的理解是否正确感到困惑



- >每个类至少需要一个定义的构造函数 - False(在C#中,如果你不提供一个,编译器会自动提供一个默认的构造函数。)



- > ;只需在其上只有一个私有构造函数就可以避免实例化一个类。 - False(私有构造函数是一个特殊的实例构造函数。它通常只在包含静态成员的类中使用。简而言之,您仍然可以通过其他方法(例如,从静态方法)创建具有私有构造函数的类的实例



- >非对象类中的静态构造函数在每个对象创建时调用一次。 - False(如果静态构造函数被调用一次,编译器将不会再次调用它,它将仅被调用一次。静态构造函数的执行由应用程序域中发生的以下第一个事件触发:1。创建类类型的实例.2。任何静态引用类类型的成员。)



- >您可以在构造函数上指定类继承的接口类

(我不知道请澄清)

I want to know whether my understanding on the constructors is correct or not. I just tried to attempt a quiz for constructors but confused whether my understanding is correct or not

--> Every class needs at least one constructor defined - False (In C#, if you don't provide one, the compiler automatically provides a default constructor.)

--> It’s possible to avoid a class being instantiated by simply having only a private constructor on it. – False (A private constructor is a special instance constructor. It is generally used in classes that contain static members only. In a nutshell, you can still create an instance of a class with private constructor thru some other means (e.g. from static methods)

--> A static constructor in a non­static class is called once on each object creation. – False (If a static constructor has been called once, compiler will not call it again, it will get called once only .The execution of a static constructor is triggered by the first of the following events to occur within an application domain: 1. An instance of the class type is created. 2. Any of the static members of the class type are referenced.)

-->You can specify on the constructor which classes of interfaces that class is inheriting
( i don't know please clarify )

推荐答案

False - 指定接口和基类在类定义上,而不是在构造函数上。
False - you specify interfaces and the base class on the class definition, not on the constructor.


这篇关于需要澄清c#中的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆