Skip to content

Instantly share code, notes, and snippets.

@pishangujeniya
Last active February 1, 2021 07:52
Show Gist options
  • Save pishangujeniya/4398db8b9374b081b0670ce746f34cbc to your computer and use it in GitHub Desktop.
Save pishangujeniya/4398db8b9374b081b0670ce746f34cbc to your computer and use it in GitHub Desktop.
Inline Interface Implementation as Java in C#
public class MainClass : IMyInterface {
public void paymentProceed () {
MyClass MyClass = new MyClass ();
// Java Inline Style
MyClass.someMethod ("INPUT TEXT", new MyClassImplementor (
(object resp) => {
Debug.WriteLine ("INTERNAL LAMBDA CALLBACK for " + resp.ToString ());
}
));
// C# Style
MyClass.someMethod ("PISHANG", this);
}
void IMyInterface.InterfaceResponse (object data) {
Debug.WriteLine ("As C# Style for "+ data.ToString ());
//throw new NotImplementedException();
}
}
public class MyClass {
public void someMethod (string id, IMyInterface _iMyInterface) {
string someResponse = "RESPONSE FOR " + id;
_iMyInterface.InterfaceResponse (someResponse);
}
}
public interface IMyInterface {
void InterfaceResponse (object data);
void InterfaceResponse2 (object data, string x);
}
public class MyInterfaceImplementor : IMyInterface {
private readonly Action<object> actionname;
private readonly Action<object, string> actionInterfaceResponse2;
public MyInterfaceImplementor (Action<object> InterfaceResponse) {
this.actionname = InterfaceResponse;
}
public MyInterfaceImplementor(Action<object> interfaceResponseMethod, Action<object, string> interfaceResponseMethod1) {
this.actionname = interfaceResponseMethod ?? throw new ArgumentNullException(nameof(interfaceResponseMethod));
this.actionInterfaceResponse2 = interfaceResponseMethod1 ?? throw new ArgumentNullException(nameof(interfaceResponseMethod1));
}
public void InterfaceResponse (object data) {
this.actionname (data);
}
public void InterfaceResponse2(object data, string x) {
this.actionInterfaceResponse2(data, x);
}
}
@pishangujeniya
Copy link
Author

interface-implementation-like-java-in-c-sharp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment