Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>House class may have a collection of features.</p> <p>You can basicly create an abstract base class called "Feature" or an interface called "IFeature" and inherit/implement it to the class that is meant to be a feature (i.e. <code>Garden</code>).</p> <p>Then all you need to do is creating a collection in <code>House</code> class called "Features".<br> Here is an example interface in C#:</p> <pre><code>interface IFeature { // Properties or methods you want all the features to have. decimal Price { get; } } </code></pre> <p>Your feature classes need to implement <code>IFeature</code> interface.</p> <pre><code>class Garden : IFeature { // This property is needed to implement IFeature interface. public decimal Price { get; private set; } public Garden(decimal price) { Price = price; } } </code></pre> <p>To implement <code>IFeature</code>, a class must have a <code>decimal</code> property called "Price" with a get accessor like the <code>Garden</code> class above and the <code>Pool</code> class below:</p> <pre><code>class Pool : IFeature { public decimal Price { get; private set; } public float Depth { get; private set; } public Pool(decimal price, float depth) { Price = price; Depth = depth; } } </code></pre> <p><code>House</code> class should have a collection of <code>IFeature</code> instead of <code>Pool</code> or <code>Garden</code>:</p> <pre><code>class House { public List&lt;IFeature&gt; Features { get; private set; } public House() { Features = new List&lt;IFeature&gt;(); } } </code></pre> <p>Then, you can add features to a house like this:</p> <pre><code>House h = new House(); h.Features.Add(new Garden(6248.12m)); h.Features.Add(new Pool(4830.24m, 10.4f)); </code></pre> <p>And using LINQ, you can,</p> <pre><code>// Check whether the house has a garden: h.Features.Any(f =&gt; f is Garden); // Get the total cost of features. h.Features.Sum(f =&gt; f.Price); // Get the pools that are deeper than 5 feet. h.Features.OfType&lt;Pool&gt;().Where(p =&gt; p.Depth &gt; 5f); // etc. </code></pre> <p><a href="http://msdn.microsoft.com/en-us/library/ms173156.aspx" rel="nofollow" title="Interfaces (C# Programming Guide) | MSDN">More information about interfaces</a>.<br> <a href="http://msdn.microsoft.com/en-us/library/bb397926" rel="nofollow" title="LINQ (Language-Integrated Query) | MSDN">More information about LINQ</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload