views:

78

answers:

2

This is something curious that I saw in my coding today.

Here is the sample code:

public class SomeClass
{
   public IUtils UtilitiesProperty { get; set; }
}

public interface IUtils
{
   void DoSomething();
}

public class Utils : IUtils
{
   void DoSomething();
}

This compiles fine.

So what is UtilitiesProperty? Is it a Util? What if more than one class implemented IUTil? Would it fail the compile then?

+11  A: 

It doesn't have any value until you give it one (or rather, it has the value null). If you assign it a Utils reference, then yes: it is a Utils, exposed via the IUtils interface. You can only give it null or things that implement IUtils.

Marc Gravell
+5  A: 

It's a property that can hold an object that implements your IUtils interface. More classes can implement this interface and using the interface allows you a level of abstraction (the consumer doesn't care as long as the class adheres to the interface contract).

I'd suggest you read up on the use of interfaces, abstract classes and the like.

For example the MSDN docs.

Wim Hollebrandse