'Make a method both static and instance [closed]
I have a class called Foo
and a static method - Bar (Foo instance)
- inside it. But instead of only being able to call it like Foo.Bar(new Foo())
, I want to also be able to say new Foo().Bar();
TL;DR I have a static method in class Foo
which has a parameter of type Foo
and I want to also be able to call it as if it was a non-static method.
This is all for testing purposes. I know this'd be terrible code design.
Solution 1:[1]
Create both methods, and let one redirect to other, where main logic is:
public class Foo
{
// Called by Foo.Bar(new Foo());
public static void Bar(Foo instance)
{
// logic here
}
// Called by new Foo().Bar();
public void Bar()
{
Foo.Bar(this);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |