How do you call a function only once in C#?
To call a function only once in C# we can define a boolean and check each time the function is called!Example
In below example we will set the isDone boolean to true after the first time it loops, the expectation is that when looping is completed it will only print the message once.using System;
public class Example
{
static bool isDone = false;
static void Update(int number) {
if (!isDone) {
Console.WriteLine("Called Only Once!");
}
}
public static void Main()
{
for(int i=0; i<100;i++){
Update(i);
isDone = true;
}
}
}
Output
Called Only Once!