有什么方法可以确定该方法是否访问默认值 [英] Is there any way to identify whether the method accessing the default value

查看:104
本文介绍了有什么方法可以确定该方法是否访问默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为myStaticMethod的静态方法.定义如下:

I''am having a static method named myStaticMethod. Which is defined as follows:

public static void myStaticMethod(string strInputVal="Default")
        {       
            if ("Is accessing Default Value") // how can i define this condition
            {
                //Do something
            }
            else
            {
                //Do some other task
            }
        }



现在,我可以按以下不同方式调用该方法:



Now i am able to call the method in different ways as follows:

myStaticMethod(); // will access default value
myStaticMethod("Some Value");// will use the passed value
myStaticMethod("Default"); // Here passing value and default value are same



在这里,我的问题是如何识别方法是访问默认值还是通过方法调用传递的值.



请注意:如果我将条件定义为类似,



Here my question is How can i identify whether the method is accessing the default value or the value passed through the method call.



Please note:If i define the condition like;

if (strInputVal == "Default")
    {
       // do operation here
    }




Which is meaning full for all function call expect myStaticMethod("Default"); because in this case the method actually accessing the passed value, but my condition will say it is accessing the default value

我尝试过的事情:




Which is meaning full for all function call expect myStaticMethod("Default"); because in this case the method actually accessing the passed value, but my condition will say it is accessing the default value

What I have tried:

myStaticMethod(); // will access default value
myStaticMethod("Some Value");// will use the passed value
myStaticMethod("Default"); // Here passing value and default value are same

推荐答案

MSDN :(尽管引号和链接均指向VB文档,但内容同样适用于C#)
MSDN: (while the quote and link are to VB documentation, the content applies equally to C#)

确定是否存在可选参数
过程无法在运行时检测是否忽略了给定参数或调用代码是否已明确提供默认值.如果需要进行区分,则可以将一个不太可能的值设置为默认值.以下过程定义了可选参数office,并测试其默认值QJZ,以查看是否在调用中将其省略:

Determining Whether an Optional Argument Is Present
A procedure cannot detect at run time whether a given argument has been omitted or the calling code has explicitly supplied the default value. If you need to make this distinction, you can set an unlikely value as the default. The following procedure defines the optional parameter office, and tests for its default value, QJZ, to see if it has been omitted in the call:

[

[^]

What you can do is to make your optional parameters nullable:

public static void SomeStaticMethod(int? intParam1 = null, bool? boolParam1 = null, string stringParam1 = null)
{
    int defaultInt = -1;
    bool defaultBool = false;
    string defaultString = "Default";

    if (intParam1 == null)
    {
        // intParam1 not specified
        // set to default ?
        // throw error ?

    } else if (intParam1 == defaultInt)
    {
        // called with default int value
        // ?
    }

    if (boolParam1 == null)
    {
        // boolParam1 not specified
        // set to default ?
        // throw error ?

    } else if (boolParam1 == defaultBool)
    {
        // called with default bool value
        // ?
    }

    if (stringParam1 == null)
    {
        // stringParam1 not specified
        // set to default ?
        // throw error ?

    } else if (stringParam1 == defaultString)
    {
        // called with default string value
        // ?
    }
}

您会注意到,类型" String(RefernceType [1]的奇怪味道")是可空的;对于您使用的ValueType,将?"添加到类型名称",如下所示.

我认为代码用户应该了解参数要求(调用约定)是合理的期望.

另一种策略是方法重载,您可以在其中定义一系列具有相同名称并返回Type,但具有不同(必需的,不是可选的)参数的方法.然后,您可以完全确定调用了哪种方法重载.但是,考虑到您有三个参数的情况,并且您想针对每种可能的情况编写一个单独的方法重载:none:a b c:ab ac bc:abc ...大量的工作!

[1]查看Eric Lippert的博客条目,并在StackOverflow上评论有关如何在.NET中管理字符串"类型的复杂性

You''ll note that Type ''String (a strange "flavor" of a RefernceType[1]) is Nullable; for the ValueTypes you use, you add the ''?'' to the Type Name as shown here.

I consider it a reasonable expectation that users of code be aware of the requirements for parameters (calling conventions).

Another strategy is method-overloading where you define a series of methods with the same names and return Types, but with different (required, not optional) parameters. Then, you can be absolutely sure which method overload was called. But, consider the case you had three parameters and you wanted to write a separate method overload for every possible case: none : a b c : ab ac bc : abc ... a lot of work !

[1] Look at Eric Lippert''s blog entries, and comments on StackOverflow about the complexity of how Type ''String is managed in .NET


这篇关于有什么方法可以确定该方法是否访问默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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