Reputation: 363
I am looking at the example from Using SignalR in WinForms and WPF in order to get an idea of how to implement SignalR in a .NET client application/dll. And I came across something that I don't get the concept of.
private IHubProxy HubProxy { get; set; }
When looking at the IHubProxy
I can see that it is an interface. I have always understood that you could not make an instance of an interface but it was rather a contract for a new class if you implement it telling the new class what methods it has to implement.
How does an interface setup as a property work?
Upvotes: 1
Views: 100
Reputation: 14896
Of course you can't make an instance for an Interface but you can make an instance of a class that implements that interface.
Let me make an example, consider the following code:
interface IEmailSender
{
void SendEmail();
}
public class EmailSender : IEmailSender
{
public void SendEmail()
{
throw new NotImplementedException();
}
public void DoOtherStuff()
{
throw new NotImplementedException();
}
}
Now the following code is perfectly legal:
IEmailSender mailSender = new EmailSender();
mailSender.SendEmail(); //Works just fine
mailSender.DoOtherStuff(); //Will raise an error at compile time
Let's build a test class:
public class Test
{
public IEmailSender MyEmailSender;
}
Now let's check the following code:
var testOBJ = new Test();
testOBJ.MyEmailSender = new IEmailSender(); //Raises a compile time error, as you outlined yourself
testOBJ.MyEmailSender = new EmailSender(); //Perfectly Legal
Basically you can asign to that property anything that implements that interface
Hope it's clear now :)
Upvotes: 1
Reputation: 1
You're right, you can't make instances of an interface.
What that code means is that the HubProxy property is of a type that inhertis the IHubProxy interface. So for example, the get returns an instance of a class that implements the IHubProxy interface.
Upvotes: 0