jeudi 24 décembre 2020

The same If statement for all methods in a class c# [closed]

Just as a sample.

Consider that I have a class with lot of methods inside.

All of those methods should do what they do after an if check (out commented at the bottom of the code)

Here Code:

public class RolesManagement
{
    public void FirstM()
    {
        Console.WriteLine("A");
    }
    
    public void SecondM()
    {
        Console.WriteLine("B");
    }
    
    public void ThirdM()
    {
        Console.WriteLine("C");
    }
    
    //Here my if statement
    //if (a == b)
    //{ 
    //     Here what FirstM(), SecondM() and ThirdM() does
    //}
    

}

Now I can do in every method the following:

public void ThirdM()
{

    if (a == b)
    {
        Console.WriteLine("C");
    }
}

But if I have a class with 100 methods its a litte bit hard.

How is the easiest way to do that?

Aucun commentaire:

Enregistrer un commentaire