可选参数由[optional]C#执行,检查参数是否已通过

可选参数由[optional]C#执行,检查参数是否已通过,c#,C#,我有这样的类构造函数: public Script(string scriptName, [Optional] ICollection<Tuple<string, bool>> internalFunctions, [Optional] long randomIdentifier) { } 或 或 打电话 谢谢。如果没有显式传递值,那么这些参数将具有默认值,您必须自己测试这些值: public Script(string scriptName, [Optional] I

我有这样的类构造函数:

public Script(string scriptName, [Optional] ICollection<Tuple<string, bool>> internalFunctions, [Optional] long randomIdentifier) { }

打电话


谢谢。

如果没有显式传递值,那么这些参数将具有默认值,您必须自己测试这些值:

public Script(string scriptName, [Optional] ICollection<Tuple<string, bool>> internalFunctions, [Optional] long randomIdentifier)
{
    if (internalFunctions != null)
    {
        // do something that needs internalFunctions to have a value
    }

    if (randomIdentifier != 0)
    {
        // do something that needs randomIdentifier to have a valid value
    }
    else
    {
        // either a value wasn't passed, or the value 0 was passed...
        //   you can't be sure, so you might want to make this nullable
    }
}
公共脚本(字符串scriptName,[可选]ICollection internalFunctions,[可选]长随机标识符)
{
if(内部函数!=null)
{
//做一些需要内部函数才能有价值的事情
}
如果(随机标识符!=0)
{
//执行需要随机标识符才能具有有效值的操作
}
其他的
{
//未传递值,或者传递了值0。。。
//您不能确定,因此您可能希望将此设置为可空
}
}
哦,那么,它是默认使用的(长)?那么,通过Nullable实现它的最佳方法是什么?
new Script("test1", null)
new Script("test1", null, 1)
public Script(string scriptName, [Optional] ICollection<Tuple<string, bool>> internalFunctions, [Optional] long randomIdentifier)
{
    if (internalFunctions != null)
    {
        // do something that needs internalFunctions to have a value
    }

    if (randomIdentifier != 0)
    {
        // do something that needs randomIdentifier to have a valid value
    }
    else
    {
        // either a value wasn't passed, or the value 0 was passed...
        //   you can't be sure, so you might want to make this nullable
    }
}