1

I have a chunck of code where methods are created like that:

class myClass{
    myClass(type arg){
        method1 =() => {do something with arg}
        method2 =() => {do something else with arg}
    }
    public readonly Action method1;
    public readonly Action method2;
}

If I understand how c# works, there 2 references of arg that are created in method1 and method2 instead of one in the object and lambda are slower. (I have heard)

What is the interest of doing that instead of that:

class myClass{
    myClass(type arg){
        this.arg=arg
    }
    private arg;
    public void method1() {do something with arg};
    public void method2(){ {do something else with arg};
}
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
  • The fields are public. So apparently the programmer thought that it might be useful for client code to replace the delegates and give the class different behavior. It is a fishy alternative to the *virtual* keyword. – Hans Passant Oct 08 '18 at 14:03

0 Answers0