Is it possible to overwrite voids in C sharp on an instance-by-instance basis?

I am trying to make a basic GUI system.

Each button belongs to a class with the void, “vclicked”. I want to overwrite this void for each button, so each button can be told to call its customized version of that void when it is clicked.

The current system I have iterates through all buttons and tells them what to do when they are clicked, depending on their index. (if 0 has been clicked, … if 1, … etc).

In Java, I’ve noticed something that does exaclty what I’m looking for:

 ary_guiele[0] = new Cls_guielement(...){
			public void vclicked(){
				<anything goes...>
                    }
 };

Each button can be told to call vclicked void, which differs for each one.

I tried to use the exact same instantiation in C sharp, but it just throws compile errors. I think I’ve seen the curly brackets after the parentheses in instantiation used for something. Maybe I’m doing something wrong, or the syntax is different, like inheritance (“a extends b” vs. “a : b”).

It’s not necessary that I find a way around my current method of storing button instructions, I just want to know if it can be done in C sharp.

I appologize if this question has been answered before. I spent a long time looking for other answers. Googling just doesn’t help when it’s hard to describe the situation; I have no idea what official term represents this.

I also heard something about unsafe editing, something about using an asterisk with a void to get a similar effect? I think it can’t be used in web-players.

I want to say thanks for all the answers I’ve read from other people’s questions that proved helpful.

You could use C# delegates. Each button could have a vclicked delegate member, to which you can then dynamically attach a different function. To create in-place code fragments without defining them as new methods, use anonymous delegates.