如何恢复在C#中使用构造函数的值? [英] How to return a value using constructor in c#?
本文介绍了如何恢复在C#中使用构造函数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我跟着的是,我不会使用任何打印语句Classes.but我已经做了参数验证,在constructors.please告诉我该怎么返回验证我在构造函数中所做的主要功能的约定......
I follow a convention that,I wont use any print statement in Classes.but I have done a parameter validation in constructors.please tell me how to return that validation which I've done in constructor to the Main function...
推荐答案
构造方法不返回一个值 - 该类型正在建设中......
The constructor does return a value - the type being constructed...
一个构造函数不应该返回任何其他类型的值。
A constructor is not supposed to return any other type of value.
当您在构造函数中验证,你应该抛出的的异常的,如果传递的值是无效的。
When you validate in a constructor, you should throw exceptions if the passed in values are invalid.
public class MyType
{
public MyType(int toValidate)
{
if (toValidate < 0)
{
throw new ArgumentException("toValidate should be positive!");
}
}
}
这篇关于如何恢复在C#中使用构造函数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文