It really depends. If the variable is of type "interface", then in this case only the type of interface can be accessed by the object.
Let's look at an example - suppose I have an interface as defined below -
interface IMyInterface
{
string B();
}
and if I implement this interface using the "MyClass" class as shown below -
public class MyClass:IMyInterface
{
public string B()
{
return "In Class";
}
}
public class MyAnotherClass:IMyInterface
{
public string B()
{
return "In Another Class";
}
}
, .
IMyInterface myinst = new MyClass();
B(), myinst, MyClass.
, , , IMyInterface, -
public class UseOfInterface{
public void InterfaceUse(IMyInterface myPara)
{
myPara.B();
}
}
, -
IMyInterface myInst = new MyClass();
IMyInterface myAnotherInst = new MyAnotherClass();
UseOfInterface interfaceUse = new UseOfInterface();
interfaceUse.InterfaceUse(myInst); // returns "In Class"
interfaceUse.InterfaceUse(myAnotherInst); // returns "In Another Class"
, , , Interface.
MyClass, MyClass, -
MyClass myinst = new MyClass();
B() , MyClass. , , .
: ?
, , #. , .
, - "" , ,
namespace CarNameSpace
{
public class Car()
{
public void Drive(IDriver driver)
{
if(driver.Age > 18)
{
driver.Drive();
}
}
}
}
, , IDriver, , ,
interface IDriver
{
string Age{get; set;}
string Name {get set;}
string Drive()
}
, , IDriver , , , , ,
public class PerfectDriver:IDriver
{
public PerfectDriver()
{
Name = "Vikram";
Age = 30;
}
public int Age{get; set;}
public string Name {get; set;}
public string Drive()
{
return "Drive perfectly";
}
}
,
PerfectDriver perf = new PerfectDriver
Car myCar = Car();
myCar.Driver(perf);