How to call an interface in C#
29,791
Solution 1
An interface cannot be instantiated by itself. You can't just call an interface. You need to instantiate a class that actually implements the interface.
Interfaces don't and can't do anything by themselves.
For example:
ISample instance = new iChild(); // iChild implements ISample
instance.fncAdd();
The following questions provide more detailed answers about this:
- Why is an interface or an abstract class useful? (or for what?)
- Interface vs Abstract Class (general OO)
Solution 2
You can't "call" interface or create instance of it.
What you can do is have your class implement the interface then use its method fncAdd
.
Solution 3
Do you mean?:
static void Main(string[] args)
{
ISample child = new iChild();
child.fncAdd();
}
Although, as stated by others the code doesn't seem like it's using inheritance correctly.
Author by
sam
Updated on March 14, 2020Comments
-
sam about 4 years
interface ISample { int fncAdd(); } class ibaseclass { public int intF = 0, intS = 0; int Add() { return intF + intS; } } class iChild : ibaseclass, ISample { int fncAdd() { int intTot = 0; ibaseclass obj = new ibaseclass(); intTot = obj.intF; return intTot; } }
I want to call
ISample
instatic void Main(string[] args)
but I dont know how to do that. Could you please tell me how?