C#构造函数 [英] C# constructor

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

问题描述



参数之一中定义一个返回值的构造函数是否正确如下 -

public constructor1(bool boolvalue,out int intvalue)

{

....

}

regrards

Ronny

Is it correct to define a constructor with a return value in one of the
paramters as follows -
public constructor1(bool boolvalue, out int intvalue)
{
....
}
regrards
Ronny

推荐答案

Ronny用他的键盘写:
Ronny used his keyboard to write :

定义带返回的构造函数是否正确其中一个

参数的值如下 -

public constructor1(bool boolvalue,out int intvalue)

{

...

}

regrards

Ronny
Is it correct to define a constructor with a return value in one of the
paramters as follows -
public constructor1(bool boolvalue, out int intvalue)
{
...
}
regrards
Ronny



它是至少*非常*不寻常,但是当你编译它时会发生什么?


为什么不用那个out值为readonly(get;私有集;)

构造类的属性?

It is at least *very* unusual, but what happens when you compile it?

Why not have that "out" value as a readonly (get; private set;)
property of the constructed class?

Hans Kesting

我从来没有见过它,但我想不出你为什么不能做这样的事情。虽然我很好奇你打算用什么

,请告诉:-)


-

Pete

====
http://mrpmorris.blogspot .com
http://www.capableobjects.com

I''ve never seen it done, but I can''t think why you shouldn''t be able to do
such a thing. Mostly though I am curious as to what you intend to use it
for, please tell :-)

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com


Peter Morris skrev:
Peter Morris skrev:

我从未见过它,但我可以我想你为什么不能这样做呢?虽然我很好奇你打算用什么来b / b
使用它,请告诉:-)
I''ve never seen it done, but I can''t think why you shouldn''t be able to
do such a thing. Mostly though I am curious as to what you intend to
use it for, please tell :-)



我可以看到很多原因,其中一个原因是返回一些

状态值,告诉构造函数实际上失败了,并且

没有理由继续下去,而且我们知道构造函数无法返回null。


经典情况:一些TakePicture diaglog和构造函数

检测到没有摄像头。


就个人而言,拥有一个get {}属性可能更聪明,你可以在开火前测试

,例如ShowDialog()。


-

Bj?rn Brox

I can see many reasons, where one of the reasons is to return some
status value telling that the constructor actually failed and there is
no reason to go further, and as we know a constructor cannot return null.

Classical situation: Some TakePicture diaglog and the constructor
detects that there is no camera present.

Personally it is probably smarter to have a get {} property you can test
on before firing for example ShowDialog().

--
Bj?rn Brox


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

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