LearnDotNEt
LearnDotNEt

Reputation:

Generics in C#

class Program
{
    static void Main(string[] args)
    {
        HomeFurniture homeFur = new HomeFurniture();
        Check<HomeFurniture,Furniture>(homeFur);
        OfficeFurniture officeFur = new OfficeFurniture();
        Check<OfficeFurniture,Furniture>(officeFur);
    }

    public static void Check<U,T>(T check) where T:Furniture
                                           where U:T
    {
        // I would like to set the properties of HomeFurniture as 
        // MyProperty = 9;
        // and that of OfficeFurniture  as 
        // MyProperty = 10;
    }


    public class HomeFurniture : Furniture
    {
        public int MyProperty { get; set; }
    }

    public class OfficeFurniture: Furniture
    {
        public int MyProperty { get; set; }
    }

    public class Furniture
    {


    }
}

Upvotes: 2

Views: 253

Answers (5)

Preet Sangha
Preet Sangha

Reputation: 65546

HomeFurniture h = check as HomeFurniture;
if(h != null) {
  if( h.MyProperty != 9) { ... do some thing ...} 
} else {
  OfficeFurniture o = check as OfficeFurniture;
  if(o != null) {
    if( o.MyProperty != 10) { ... do some thing ...} 
  }
} 

Upvotes: 1

dtb
dtb

Reputation: 217371

If Check depended on the concrete type of furniture, it wouldn't be generic anymore.

Simple single dispatch should be all you need here:

public void Check(HomeFurniture f)
{
    f.MyProperty = 9;
}

public void Check(OfficeFurniture f)
{
    f.MyProperty = 10;
}

Upvotes: 2

John Rasch
John Rasch

Reputation: 63485

Why don't you declare MyProperty in the Furniture class and then override it?

Example:

public class Furniture
{
    public virtual int MyProperty {get;}
}

public class HomeFurniture : Furniture
{
    public override int MyProperty
    {
        get
        {
            return 9;
        }
    }
}

public class OfficeFurniture : Furniture
{
    public override int MyProperty
    {
        get
        {
            return 10;
        }
    }
}

Upvotes: 3

Andrew Hare
Andrew Hare

Reputation: 351648

Unfortunately generics aren't going to help you here. The best way to handle this is to change Check to be something like this:

public static void Check(Furniture check)
{
    HomeFurniture homeFurniture = check as HomeFurniture;

    if (homeFurniture != null)
    {
        homeFurniture.MyProperty = 9;
        return;
    }

    OfficeFurniture officeFurniture = check as OfficeFurniture;

    if (officeFurniture != null)
    {
        officeFurniture.MyProperty = 10;
        return;
    }
}

Upvotes: 1

Ron Warholic
Ron Warholic

Reputation: 10074

If I understand your question, you want to specialize your generic function for each type of furniture?

If so, sad to say, C# does not support specialization of generics. You are limited to implementing your function using the interface guaranteed by your 'where' clauses.

You may also examine your types using reflection but that will be cludgey and hard to maintain (and totally defeat the purpose of inheritance).

I recommend using your Furniture class to declare a virtual method to set your property which you override in your derived HomeFurniture and OfficeFurniture classes.

For example:

public class Furniture {
  public virtual void SetProperty(int val);
}

public class HomeFurniture  {
  public override void SetProperty(int val)  { // do homefurniture specific things 
  }
}

And so forth.

Upvotes: 0

Related Questions