Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, May 26, 2016

Generics in C#

Generics in C#


class Program  {      static void Main(string[] args)      {          HomeFurniture homeFur = new HomeFurniture();          Check(homeFur);          OfficeFurniture officeFur = new OfficeFurniture();          Check(officeFur);      }        public static void Check(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      {          }  }  

Answer by Preet Sangha for Generics in C#


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 ...}     }  }  

Answer by Ron Warholic for Generics in C#


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.

Answer by Andrew Hare for Generics in C#


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;  	}  }  

Answer by John Rasch for Generics in C#


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;          }      }  }  

Answer by dtb for Generics in C#


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;  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.