Beau D'Amore
Beau D'Amore

Reputation: 3404

.NET: class definition explanation

I understand basic inheritance and I understand the basics of generics.

But I do NOT understand this class definition:

public class ExportController : AbstractFeedController<IExportFeed>

The ExportController inherits AbstractFeedController...

but, what does the <IExportFeed> do/mean? Is it something to do with generics?

Upvotes: 0

Views: 124

Answers (5)

Dmitry
Dmitry

Reputation: 344

Imagine the situation like

public class ExportController : IExportFeed
public class ImportController : IImportFeed

let's assume that in export case we have some operations common for all implementers of IExportFeed. So may move these operations into some base abstract class for IExportFeed hierarchy. Same for IImportFeed.

But what if we have some common operations for both these hierarchies? we can do something like

public abstract class ImportExportController : IExportFeed, IImportFeed

and inherit export or import classes from this one.

But this design breaks minimum couple of SOLID principles, and it's going to be a mess if you decide to add anoter feed interfaces

The solution is to move this common inter-hierarchy functionality into a generic(template)

public class ExportController : AbstractFeedController<IExportFeed>
public class ImportController : AbstractFeedController<IImportFeed>

etc

Upvotes: 1

Marshal
Marshal

Reputation: 6671

In plain english it means that ExportController is deriving from a closed generic typed class AbstractFeedController<IExportFeed>. AbstractFeedController class has some of the methods, properties, fields, indexers etc whose types or return types or parameter types could be of type IExportFeed .

So AbstractFeedController class may look like this

//This is a open type:
public class AbstractFeedController <T>
{
   T[] m_Items; 
   public void Feed(T item)
   {...}
   public T ReturnFeed()
   {...}
}

Now we close the type by intansiating a class with IExportFeed as generic type parameter

AbstractFeedController feedController = new AbstractFeedController<IExportFeed>();

So the class internally translates as follows:

//This is a closed type now:
public class AbstractFeedController <IExportFeed>
{
   IExportFeed[] m_Items;  //Indexer type of IExportFeed
   public void Feed(IExportFeed item) //A method accepting a parameter of type IExportFeed
   {...}
   public IExportFeed ReturnFeed() //A method returning type of IExportFeed
   {...}
}

Upvotes: 2

Tim Bee
Tim Bee

Reputation: 2230

Yes, it is a generic definition. In short, AbstractFeedController defines a generic implementation that can be applied to various Types including IExportFeed in your case.

Look at the definition of the class AbstractFeedController, you will likely see something like

class AbstractFeedController<T>{ ...

In the class you will then see the Type T used multiple times. Whenever you see this T, you can swap it in your mind with any Type you think can apply.

In the class definition, you might also see a where T : .... This is a condition on the Type T, limiting the kind of Types the class can use.

Read this MSDN Article for in-depth explanation.

Upvotes: 2

Dovydas Šopa
Dovydas Šopa

Reputation: 2300

An Introduction to C# Generics chapter Inheritance and Generics:

When deriving from a generic base class, you must provide a type argument instead of the base-class's generic type parameter:

public class BaseClass<T>
{...}
public class SubClass : BaseClass<int>
{...}

If the subclass is generic, instead of a concrete type argument, you can use the subclass generic type parameter as the specified type for the generic base class:

public class SubClass<T> : BaseClass<T> 
{...}

This means that your class ExportController is no longer generic and is derived from class AbstractFeedController<IExportFeed>.

Upvotes: 4

Martin Moser
Martin Moser

Reputation: 6267

The inherited class implicitly defines the type of the generic class.

So the inherited class is not generic anymore, and uses the base class for the provided type.

Upvotes: 0

Related Questions