简体   繁体   English

[Optional] C# 的可选参数,检查 args 传递

[英]optional parameters by [Optional] C#, check args passed

I have class constructor like this:我有这样的类构造函数:

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

So, how it possible to check, were something passed in randomIdentifier or internalFunctions?那么,如何检查,是否在 randomIdentifier 或 internalFunctions 中传递了某些内容? So, was所以,被

new Script("test1")

or或者

new Script("test1", null)

or或者

new Script("test1", null, 1)

called?叫?

Thank you.谢谢你。

If a value was not explicitly passed, then those parameters will have their default values, and you'll have to test for those values yourself:如果未明确传递值,则这些参数将具有其默认值,您必须自己测试这些值:

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM