Is there a way in C # to call a method only once, like in jQuery a “one” method?

I would like to know if there is a way to execute code in C # only once, like "one" in jquery:

$ ("# foo"). one ("click", function () {alert ("This will only be displayed once.");});

I would like to do the following:

public void foo(){ Console.Write("hello"); } 

then

 foo(); foo(); foo(); 

and the output should be

 hello 

I am looking for a library, not just using flag attributes.

+9
source share
7 answers

I can’t imagine why doing something like this, but if you really do, and if you want it to be universal for any method, you can do this:

 void Main() { var myFoo = callOnlyOnce(foo); myFoo(); myFoo(); myFoo(); var myBar = callOnlyOnce(bar); myBar(); myBar(); myBar(); } void foo(){ Console.Write("hello"); } void bar() { Console.Write("world"); } Action callOnlyOnce(Action action){ var context = new ContextCallOnlyOnce(); Action ret = ()=>{ if(false == context.AlreadyCalled){ action(); context.AlreadyCalled = true; } }; return ret; } class ContextCallOnlyOnce{ public bool AlreadyCalled; } 
+13
source

The jQuery example is an event handler, and once the event handler has been called, it is removed from the element. Equivalent in C # for (e.g..) Button click events will be

 myButton.Click += new EventHandler(MyEventHandler) void MyEventHandler(object sender, EventArgs e) { Console.Write("hello"); ((Button)sender).Click -= new EventHandler(MyEventHandler); } 

Thus, only the first click of a button will result in recording to the console.

+16
source

Why do you want a library when you can just do something like this:

 private bool wasExecuted = false; public void foo(){ if (!wasExecuted) { Console.Write("hello"); wasExecuted = true; } } 
+6
source
 private bool _fooExecuted = false; public void foo(){ if (_fooExecuted) return; _fooExecuted = true; Console.Write("hello"); } 
+3
source

I don’t think you will find a library for this. This is, at best, very rare. The easiest way to achieve this is to simply have a variable of type static like this:

 static bool hasRun = false; public void doSomething { if (hasRun) { return; } } 
+2
source

Do you mean only once during the entire execution of the program, or will you ever have a reset criterion that allows it to be displayed again? In any case, I recommend creating a wrapper class that you create as a field with a built-in bool flag for each particular line that you want to display only "once." If the line is known as fixed (without parameters that change it), you can also use the central service class, which stores a set of lines that have already been printed. The problem is that you cannot register the same line from multiple places.

One general general solution is the "alarm manager", although I have not seen a standardized library for this. However, this often occurs in my industry, because industrial equipment can enter a state of constant alarm, and you want to register transitions to an alarm state, but you do not constantly register that you are still in an alarm state. The solutions I used / saw usually create a unique alarm code for each condition with the concepts of "set" and "clear", often with the intervention of the operator, necessary to confirm and finally eliminate the alarm.

+1
source

I know this is old news ... but here is a functional approach to a question that someone might find useful:

  public static Action<T> Once<T>(Action<T> action ) { return (arg) => { action?.Invoke(arg); action = null; }; } 

Then you can generate a function that will do all the work only once:

  var writeNumberToConsole = Once<int>( Console.WriteLine ); writeNumberToConsole(1); // "1" writeNumberToConsole(2); -> //nothing writeNumberToConsole(3); -> //nothing 
0
source

Source: https://habr.com/ru/post/1347275/


All Articles