Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#当func被多次调用时,如何调用子函数一次_C#_Func - Fatal编程技术网

C#当func被多次调用时,如何调用子函数一次

C#当func被多次调用时,如何调用子函数一次,c#,func,C#,Func,从一些例子开始: public abstract class T { void subfunc() { /*code*/ } void func() subfunc(); //code } 当我多次调用main方法T.func()时,我需要只在第一次调用func()时才调用subfunc()。我真的不想在main方法中调用它,因为它在逻辑上不引用它。在func中传递一个参数,该参数说明是否需要调用subFunc callSubFunc = true; func(callSubF

从一些例子开始:

public abstract class T
{

void subfunc() { /*code*/ }

void func()
   subfunc();
   //code
}

当我多次调用main方法T.func()时,我需要只在第一次调用func()时才调用subfunc()。我真的不想在main方法中调用它,因为它在逻辑上不引用它。

在func中传递一个参数,该参数说明是否需要调用subFunc

callSubFunc = true;
func(callSubFunc);
callSubFunc = false;
//further calls of func(callSubFunc) will pass false everytime
并对func进行如下修改:-

void func(bool shouldCallSubFunc)
{
    if(shouldCallSubFunc)
        subFunc()
}

希望这有助于

保留一个标志,以检查subfunc()是否已被调用一次。例如,如果(isFirstTime){subfunc();isFirstTime=false;}或者如果您需要获取某种类型的值。。。