如何判断方法是否访问默认值
How to identify whether the method accessing the default value or not
我有一个名为 myStaticMethod
的静态方法。其中定义如下:
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
}
}
现在我可以通过以下不同方式调用该方法:
myStaticMethod(); // will access default value
myStaticMethod("Some Value");// will use the passed value
myStaticMethod("Default"); // Here passing value and default value are same
我的问题是如何确定该方法是访问默认值还是通过方法调用传递的值。
如果我这样定义条件;
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
+1 给@Sinatr 我只会添加说明。:
public static void myStaticMethod(string strInputVal)
{
// Do what same part
}
// When you call this method it is
public static void myStaticMethod()
{
string strInputVal = "Default";
//Do something for default value
System.Console.WriteLine("I am indetified as called with default value");
myStaticMethod(strInputVal);
}
正如Sinatr在评论中提到的,重载方法很可能是最有效的方法。然后两个重载都可以将控制集中到一个内部方法中,指示是否使用了默认值:
public static void myStaticMethod()
{
myInternalStaticMethod ("Default", true);
}
public static void myStaticMethod(string inputValue)
{
myInternalStaticMethod (inputValue, false);
}
private static void myInternalStaticMethod(string inputValue, bool isDefault)
{
if (isDefault) {
//Do something
} else {
//Do some other task
}
}
我有一个名为 myStaticMethod
的静态方法。其中定义如下:
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
}
}
现在我可以通过以下不同方式调用该方法:
myStaticMethod(); // will access default value
myStaticMethod("Some Value");// will use the passed value
myStaticMethod("Default"); // Here passing value and default value are same
我的问题是如何确定该方法是访问默认值还是通过方法调用传递的值。
如果我这样定义条件;
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
+1 给@Sinatr 我只会添加说明。:
public static void myStaticMethod(string strInputVal)
{
// Do what same part
}
// When you call this method it is
public static void myStaticMethod()
{
string strInputVal = "Default";
//Do something for default value
System.Console.WriteLine("I am indetified as called with default value");
myStaticMethod(strInputVal);
}
正如Sinatr在评论中提到的,重载方法很可能是最有效的方法。然后两个重载都可以将控制集中到一个内部方法中,指示是否使用了默认值:
public static void myStaticMethod()
{
myInternalStaticMethod ("Default", true);
}
public static void myStaticMethod(string inputValue)
{
myInternalStaticMethod (inputValue, false);
}
private static void myInternalStaticMethod(string inputValue, bool isDefault)
{
if (isDefault) {
//Do something
} else {
//Do some other task
}
}